Pad string : String Concatenation « Data Type « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Data Type » String Concatenation 
2. 22. 7. Pad string
/*
 * Static String formatting and query routines.
 * Copyright (C) 2001-2005 Stephen Ostermiller
 * http://ostermiller.org/contact.pl?regarding=Java+Utilities
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * See COPYING.TXT for details.
 */


import java.util.HashMap;
import java.util.regex.Pattern;

/**
 * Utilities for String formatting, manipulation, and queries.
 * More information about this class is available from <a target="_top" href=
 * "http://ostermiller.org/utils/StringHelper.html">ostermiller.org</a>.
 *
 @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
 @since ostermillerutils 1.00.00
 */
public class StringHelper {

  /**
   * Pad the beginning of the given String with spaces until
   * the String is of the given length.
   
   * If a String is longer than the desired length,
   * it will not be truncated, however no padding
   * will be added.
   *
   @param s String to be padded.
   @param length desired length of result.
   @return padded String.
   @throws NullPointerException if s is null.
   *
   @since ostermillerutils 1.00.00
   */
  public static String prepad(String s, int length){
    return prepad(s, length, ' ');
  }

  /**
   * Pre-pend the given character to the String until
   * the result is the desired length.
   
   * If a String is longer than the desired length,
   * it will not be truncated, however no padding
   * will be added.
   *
   @param s String to be padded.
   @param length desired length of result.
   @param c padding character.
   @return padded String.
   @throws NullPointerException if s is null.
   *
   @since ostermillerutils 1.00.00
   */
  public static String prepad(String s, int length, char c){
    int needed = length - s.length();
    if (needed <= 0){
      return s;
    }
    char padding[] new char[needed];
    java.util.Arrays.fill(padding, c);
    StringBuffer sb = new StringBuffer(length);
    sb.append(padding);
    sb.append(s);
    return sb.toString();
  }

  /**
   * Pad the end of the given String with spaces until
   * the String is of the given length.
   
   * If a String is longer than the desired length,
   * it will not be truncated, however no padding
   * will be added.
   *
   @param s String to be padded.
   @param length desired length of result.
   @return padded String.
   @throws NullPointerException if s is null.
   *
   @since ostermillerutils 1.00.00
   */
  public static String postpad(String s, int length){
    return postpad(s, length, ' ');
  }

  /**
   * Append the given character to the String until
   * the result is  the desired length.
   
   * If a String is longer than the desired length,
   * it will not be truncated, however no padding
   * will be added.
   *
   @param s String to be padded.
   @param length desired length of result.
   @param c padding character.
   @return padded String.
   @throws NullPointerException if s is null.
   *
   @since ostermillerutils 1.00.00
   */
  public static String postpad(String s, int length, char c){
    int needed = length - s.length();
    if (needed <= 0){
      return s;
    }
    char padding[] new char[needed];
    java.util.Arrays.fill(padding, c);
    StringBuffer sb = new StringBuffer(length);
    sb.append(s);
    sb.append(padding);
    return sb.toString();
  }

  /**
   * Pad the beginning and end of the given String with spaces until
   * the String is of the given length.  The result is that the original
   * String is centered in the middle of the new string.
   *
   * If the number of characters to pad is even, then the padding
   * will be split evenly between the beginning and end, otherwise,
   * the extra character will be added to the end.
   
   * If a String is longer than the desired length,
   * it will not be truncated, however no padding
   * will be added.
   *
   @param s String to be padded.
   @param length desired length of result.
   @return padded String.
   @throws NullPointerException if s is null.
   *
   @since ostermillerutils 1.00.00
   */
  public static String midpad(String s, int length){
    return midpad(s, length, ' ');
  }

  /**
   * Pad the beginning and end of the given String with the given character
   * until the result is  the desired length.  The result is that the original
   * String is centered in the middle of the new string.
   
   * If the number of characters to pad is even, then the padding
   * will be split evenly between the beginning and end, otherwise,
   * the extra character will be added to the end.
   *
   * If a String is longer than the desired length,
   * it will not be truncated, however no padding
   * will be added.
   *
   @param s String to be padded.
   @param length desired length of result.
   @param c padding character.
   @return padded String.
   @throws NullPointerException if s is null.
   *
   @since ostermillerutils 1.00.00
   */
  public static String midpad(String s, int length, char c){
    int needed = length - s.length();
    if (needed <= 0){
      return s;
    }
    int beginning = needed / 2;
    int end = beginning + needed % 2;
    char prepadding[] new char[beginning];
    java.util.Arrays.fill(prepadding, c);
    char postpadding[] new char[end];
    java.util.Arrays.fill(postpadding, c);
    StringBuffer sb = new StringBuffer(length);
    sb.append(prepadding);
    sb.append(s);
    sb.append(postpadding);
    return sb.toString();
  }

}
2. 22. String Concatenation
2. 22. 1. String concatenation: '+' operation generates a new String object
2. 22. 2. String concatenation: The + contains null
2. 22. 3. String concatenation: Convert an integer to String and join with two other strings
2. 22. 4. String concatenation: Combining a string and integers
2. 22. 5. String concatenation: Combining integers and a string
2. 22. 6. Substring replacement.
2. 22. 7. Pad string
2. 22. 8. Padded String
2. 22. 9. Return a string padded with the given string for the given count.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.