org.afcs.warts.util
Class StringUtils

java.lang.Object
  extended byorg.afcs.warts.util.StringUtils

public final class StringUtils
extends java.lang.Object

The StringUtils class provides various utility methods for string processing.

LICENSE: This code is released to the public domain and may be used for any purpose whatsoever without permission or acknowledgment.

Version:
Last Modified 20 August 2003
Author:
Warren Hedley ( whedley at sdsc dot edu )

Method Summary
static java.lang.String encodeAsHex(byte[] bytes)
          This method takes in an array of bytes and returns a string containing the hexadecimal representation of those bytes.
static java.lang.String leftPad(java.lang.String text, int size)
          Returns the specified text, padded on the left with whitespace (0#x20) characters to make it the specified length.
static java.lang.String leftPad(java.lang.String text, int size, char padChar)
          Returns the specified text, padded on the left with the specified padding character to make it the specified length.
static java.lang.String rightPad(java.lang.String text, int size)
          Returns the specified text, padded on the right with whitespace (0#x20) characters to make it the specified length.
static java.lang.String rightPad(java.lang.String text, int size, char padChar)
          Returns the specified text, padded on the right with the specified padding character to make it the specified length.
static java.lang.String truncateToMaxLength(java.lang.String inputString, int maxLength)
          If the string received is longer than the maximum length specified, then it is truncated, and a '...' replaces the last three characters to indicate that truncation occurred.
static java.lang.String truncateUtf8String(java.lang.String text, int maxLengthInBytes)
          Returns the specified string truncated to the specified maximum length in bytes (when encoded in UTF-8 format), with an ellipses (...) if need be.
static java.lang.String wrapAt(java.lang.String input, int wrapAt, int indent)
          This method returns a formatted version of the input string with newlines (\n) inserted every wrapAt characters.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

encodeAsHex

public static java.lang.String encodeAsHex(byte[] bytes)
This method takes in an array of bytes and returns a string containing the hexadecimal representation of those bytes. This string will be twice as long as the number of bytes in the input array.

Parameters:
bytes - The bytes to encode.
Returns:
A string containing the hex encoding of the bytes.

leftPad

public static java.lang.String leftPad(java.lang.String text,
                                       int size)
Returns the specified text, padded on the left with whitespace (0#x20) characters to make it the specified length.

Parameters:
text - The text to pad. If this is null, then size whitespace characters will be returned. If this is longer than size, then it is returned unmodified.
size - The length of the returned string (if the length of text is less than this).
Returns:
The specified text, padded on the left with whitespace (0#x20) characters to make it the specified length.

leftPad

public static java.lang.String leftPad(java.lang.String text,
                                       int size,
                                       char padChar)
Returns the specified text, padded on the left with the specified padding character to make it the specified length.

Parameters:
text - The text to pad. If this is null, then size whitespace characters will be returned. If this is longer than size, then it is returned unmodified.
size - The length of the returned string (if the length of text is less than this).
padChar - The character to pad the text with.
Returns:
The specified text, padded on the left with the specified padding character to make it the specified length.

rightPad

public static java.lang.String rightPad(java.lang.String text,
                                        int size)
Returns the specified text, padded on the right with whitespace (0#x20) characters to make it the specified length.

Parameters:
text - The text to pad. If this is null, then size whitespace characters will be returned. If this is longer than size, then it is returned unmodified.
size - The length of the returned string (if the length of text is less than this).
Returns:
The specified text, padded on the right with whitespace (0#x20) characters to make it the specified length.

rightPad

public static java.lang.String rightPad(java.lang.String text,
                                        int size,
                                        char padChar)
Returns the specified text, padded on the right with the specified padding character to make it the specified length.

Parameters:
text - The text to pad. If this is null, then size whitespace characters will be returned. If this is longer than size, then it is returned unmodified.
size - The length of the returned string (if the length of text is less than this).
padChar - The character to pad the text with.
Returns:
The specified text, padded on the right with the specified padding character to make it the specified length.

truncateToMaxLength

public static java.lang.String truncateToMaxLength(java.lang.String inputString,
                                                   int maxLength)
If the string received is longer than the maximum length specified, then it is truncated, and a '...' replaces the last three characters to indicate that truncation occurred. If the string's length is less than the specified maximum, it is returned unchanged. If a null input string is received, a null string is returned. Maximum lengths of less than 1 will be ignored.

Parameters:
inputString - The string to truncate, if necessary.
maxLength - The maximum length of the string, after truncation.
Returns:
A string at most maxLength characters long taken from the input string, or the input string if it is shorter than the specified maximum length.

truncateUtf8String

public static java.lang.String truncateUtf8String(java.lang.String text,
                                                  int maxLengthInBytes)
Returns the specified string truncated to the specified maximum length in bytes (when encoded in UTF-8 format), with an ellipses (...) if need be.

Parameters:
text - The text to truncate.
maxLengthInBytes - The maximum length in bytes.
Returns:
The specified string truncated to the specified maximum length in bytes.
Throws:
java.lang.NullPointerException - If text is null.
java.lang.IllegalArgumentException - If maxLengthInBytes is less than 3.

wrapAt

public static java.lang.String wrapAt(java.lang.String input,
                                      int wrapAt,
                                      int indent)
This method returns a formatted version of the input string with newlines (\n) inserted every wrapAt characters. An indent can be used to push out all lines other than the first (which will make them longer than wrapAt characters).

Parameters:
input - The string to format.
wrapAt - The number of characters in a line.
indent - The number of spaces to insert at the start of all lines other than the first.
Returns:
The formatted version of input, or null if input is null.
Throws:
java.lang.IllegalArgumentException - If wrapAt is less than 5, indent is less than 0, indent is greater than or equal to wrapAt, or indent is greater than 30. The first and last constraints are arbitrary and could be trivially worked around.