Pad string : String Pad « Data Type « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Data Type » String PadScreenshots 
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.
   * <p>
   * 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.
   * <p>
   * 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.
   * <p>
   * 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.
   * <p>
   * 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.
   * <p>
   * 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.
   * <p>
   * 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.
   * <p>
   * 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.
   * <p>
   * 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();
  }

}

   
    
    
    
    
  
Related examples in the same category
1.Left pad a String with a specified String.
2.Left pad a String with a specified character.
3.Left pad a String with spaces (' ').
4.Right pad a String with a specified String.
5.Right pad a String with a specified character.
6.Right pad a String with spaces (' ').
7.Padding and triming strings
8.Padded String
9.Set string length, padding with character if the shorter, or truncating if longer
10.Returns the quoted version of the string using the quotechar argument.
11.Return a string padded with the given string for the given count.
12.Left pad the given text parameter
13.Right pad the given text parameter
14.Left padding
15.Right padding
16.Pads the string with 0x and zeros on the left until it has the requested size.
17.Pads the string with zeros on the left until it has the requested size.
18.Right, left justify the string
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.