StringUtil (fawkeZ - Developer's Documentation)

org.jcoderz.commons.util
Class StringUtil

  extended by org.jcoderz.commons.util.StringUtil

public final class StringUtil
extends

This class provides string related utility functions.

Author:
Michael Griffel

Field Summary
static  EMPTY_STRING
          The empty string "".
 
Method Summary
static  asciiToString(byte[] bytes)
          Converts a byte array to a String using ASCII encoding.
static  asciiToString(byte[] bytes, int offset, int length)
          Converts a byte array to a String using ASCII encoding.
static boolean contains( str,  subString)
          Returns true if the first argument string contains the second argument string, and otherwise returns false.
static boolean equals( a,  b)
          Returns true if the two specified strings are equal to one another.
static  fitToLength( s, int minLength, int maxLength)
          Pads or truncates the given argument to be at least minLength chars and at most maxLength chars long.
static boolean isAscii(char c)
          Tests if the given character is an ASCII character, i.e. if it's integer value is less than or equal to 127.
static boolean isAscii( c)
          Determines if the specified string consists only of ASCII characters.
static boolean isBlankOrNull( s)
          Returns true if given string is null, the length is zero (empty string) or if it only contains white spaces.
static boolean isEmptyOrNull( s)
          Returns true if given string is null or the length is zero (empty string).
static boolean isNullOrBlank( s)
          Returns true if given string is null, the length is zero (empty string) or if it only contains white spaces.
static boolean isNullOrEmpty( s)
          Returns true if given string is null or the length is zero (empty string).
static  padLeft( s, char pad, int size)
          Adds the character pad to the left-side of the string s until the string size will be size.
static byte[] toBytes( s)
          Converts a String to an byte array using UTF-8 encoding.
static  toString(byte[] bytes)
          Converts a byte array to a String using UTF-8 encoding.
static  toString(byte[] bytes, int offset, int length)
          Converts a byte array to a String using UTF-8 encoding.
static  trimLength( str, int maxLength)
          Trims the length of the given string to the given maxlength, if the string length is below the given maxlength the string returned unmodified.
 
Methods inherited from class java.lang.
, , , , , , , , , ,
 

Field Detail

EMPTY_STRING

public static final  EMPTY_STRING
The empty string "".

See Also:
Constant Field Values
Method Detail

toString

public static  toString(byte[] bytes)
Converts a byte array to a String using UTF-8 encoding. If the given byte array is null, the returned string is also null.

Parameters:
bytes - The byte array to be converted.
Returns:
The string representation of the byte array or null if the given byte array is null.

toString

public static  toString(byte[] bytes,
                              int offset,
                              int length)
                       throws 
Converts a byte array to a String using UTF-8 encoding. If the given byte array is null, the returned string is also null.

Parameters:
bytes - The byte array to be converted.
offset - The index of the first byte to encode.
length - The number of bytes to encode.
Returns:
The string representation of the byte array or null if the given byte array is null.
Throws:
- if the UTF-8 encoding is not supported by the JDK.

toBytes

public static byte[] toBytes( s)
                      throws 
Converts a String to an byte array using UTF-8 encoding.

Parameters:
s - The string to be converted.
Returns:
The bytes of the given string in UTF-8 encoding.
Throws:
- if the UTF-8 encoding is not supported by the JDK.

asciiToString

public static  asciiToString(byte[] bytes,
                                   int offset,
                                   int length)
Converts a byte array to a String using ASCII encoding. If the given byte array is null, the returned string is also null.

Parameters:
bytes - The byte array to be converted.
offset - The index of the first byte to encode.
length - The number of bytes to encode.
Returns:
The String representation of the byte array.

asciiToString

public static  asciiToString(byte[] bytes)
Converts a byte array to a String using ASCII encoding. If the given byte array is null, the returned string is also null.

Parameters:
bytes - The byte array to be converted.
Returns:
The String representation of the byte array.

isAscii

public static boolean isAscii(char c)
Tests if the given character is an ASCII character, i.e. if it's integer value is less than or equal to 127.

Parameters:
c - the character to test.
Returns:
true if c <= 127, false otherwise.

isAscii

public static boolean isAscii( c)
Determines if the specified string consists only of ASCII characters.

Parameters:
c - the characters to check.
Returns:
true if the specified characters are 7-bit ASCII clean; false otherwise.

isNullOrEmpty

public static boolean isNullOrEmpty( s)
Returns true if given string is null or the length is zero (empty string).

Parameters:
s - the string to test.
Returns:
Returns true if given string is null or the length is zero (empty string); false otherwise.

isEmptyOrNull

public static boolean isEmptyOrNull( s)
Returns true if given string is null or the length is zero (empty string).

Parameters:
s - the string to test.
Returns:
Returns true if given string is null or the length is zero (empty string); false otherwise.

isNullOrBlank

public static boolean isNullOrBlank( s)
Returns true if given string is null, the length is zero (empty string) or if it only contains white spaces. The whitespace check is done using Character.isWhitespace().

Parameters:
s - the string to test.
Returns:
Returns true if given string is null, the length is zero (empty string) or the String contains only whitespace characters; false otherwise.

isBlankOrNull

public static boolean isBlankOrNull( s)
Returns true if given string is null, the length is zero (empty string) or if it only contains white spaces. The whitespace check is done using Character.isWhitespace().

Parameters:
s - the string to test.
Returns:
Returns true if given string is null, the length is zero (empty string) or the String contains only whitespace characters; false otherwise.

equals

public static boolean equals( a,
                              b)
Returns true if the two specified strings are equal to one another. Two strings a and b are considered equal if (a==null ? b == null : a.equals(b)). Also, two string references are considered equal if both are null.

Parameters:
a - one string to be tested for equality.
b - the other string to be tested for equality.
Returns:
true if the two strings are equal; false otherwise.

fitToLength

public static  fitToLength( s,
                                 int minLength,
                                 int maxLength)
                          throws ArgumentMalformedException
Pads or truncates the given argument to be at least minLength chars and at most maxLength chars long.

Parameters:
s - the string to pad or truncate
minLength - the minimum length of the string
maxLength - the maximum length of the string
Returns:
a string padded with spaces if its length is less than minLength, or truncated to be no longer than maxLength, or the string itself if its length is between minLength and maxLength
Throws:
ArgumentMalformedException - if the string argument is null, or if minLength is greater than maxLength

padLeft

public static  padLeft( s,
                             char pad,
                             int size)
Adds the character pad to the left-side of the string s until the string size will be size.

Parameters:
s - the string to pad.
pad - the padding character.
size - the final string size.
Returns:
the padded string.

trimLength

public static  trimLength( str,
                                int maxLength)
Trims the length of the given string to the given maxlength, if the string length is below the given maxlength the string returned unmodified.

Parameters:
str - the string to trim.
maxLength - the maximum length
Returns:
the string, trimt to a maximum length of maxLength

contains

public static boolean contains( str,
                                subString)
Returns true if the first argument string contains the second argument string, and otherwise returns false. This is no regular expression matching!

Parameters:
str - the string to test.
subString - the substring to look for in str.
Returns:
true if the first argument string contains the second argument string, and otherwise returns false.


Copyright 2007 The jCoderZ Project.