StringUtilities

public final class StringUtilities

A StringUtilities is a convenient class for handling some common text processing.

Author:Hugo Y. K. Lam

Fields

LINE_SEPARATOR

public static final String LINE_SEPARATOR

Methods

addLeadingCharacter

public static String addLeadingCharacter(String s, char c, int len)

Adds specified leading characters to the specified length. Nothing will be done if the length of the given string is equal to or greater than the specified length.

Parameters:
  • s – the source string.
  • c – the leading character(s) to be added.
  • len – the length of the target string.
Returns:

the string after adding the specified leading character(s).

addLeadingSpace

public static String addLeadingSpace(String s, int len)

Adds leading spaces to the given string to the specified length. Nothing will be done if the length of the given string is equal to or greater than the specified length.

Parameters:
  • s – the source string.
  • len – the length of the target string.
Returns:

the string after adding leading spaces.

addLeadingZero

public static String addLeadingZero(String s, int len)

Adds leading zeros to the given string to the specified length. Nothing will be done if the length of the given string is equal to or greater than the specified length.

Parameters:
  • s – the source string.
  • len – the length of the target string.
Returns:

the string after adding leading zeros.

addPrefix

public static String addPrefix(String s, String prefix)

Adds a prefix to a given string if it does not start with.

Parameters:
  • s – the string to be added.
  • prefix – the prefix.
Returns:

the string with the specified prefix.

addSuffix

public static String addSuffix(String s, String suffix)

Adds a suffix to a given string if it does not end with.

Parameters:
  • s – the string to be added.
  • suffix – the suffix.
Returns:

the string with the specified suffix.

appendCharacter

public static String appendCharacter(String s, char c, int len)

Appends specified characters to the given string to the specified length. Nothing will be done if the length of the given string is equal to or greater than the specified length.

Parameters:
  • s – the source string.
  • c – the character(s) to be appended.
  • len – the length of the target string.
Returns:

@return the string after appending the specified character(s).

appendSpace

public static String appendSpace(String s, int len)

Appends spaces to the given string to the specified length. Nothing will be done if the length of the given string is equal to or greater than the specified length.

Parameters:
  • s – the source string.
  • len – the length of the target string.
Returns:

@return the string after appending spaces.

appendZero

public static String appendZero(String s, int len)

Appends zeros to the given string to the specified length. Nothing will be done if the length of the given string is equal to or greater than the specified length.

Parameters:
  • s – the source string.
  • len – the length of the target string.
Returns:

the string after appending zeros.

concat

public static String concat(String[] tokens, String delim)

Concatenates a string array (string tokens) into a string with the specified delimiter string.

Parameters:
  • tokens – a string array to be concatenated.
  • delim – the delimiter.
Returns:

the concatenated string.

concat

public static String concat(String[] tokens, String tokenPrefix, String tokenSuffix, String delim)

Concatenates a string array (string tokens) into a string with the specified delimiter string, token’s prefix, and token’s suffix.

Parameters:
  • tokens – a string array to be concatenated.
  • tokenPrefix – the token’s prefix to be concatenated.
  • tokenSuffix – the token’s suffix to be concatenated.
  • delim – the delimiter.
Returns:

the concatenated string.

contains

public static boolean contains(String[] tokens, String target)

Checks if a given string array contains the specified search string.

Parameters:
  • tokens – a string array to be searched.
  • target – the target search string.
Returns:

true if the given string array contains the specified search string, false otherwise.

getLength

public static int getLength(String s)

Gets the length of the given string.

Parameters:
  • s – the string.
Returns:

the length of the given string or 0 if it is null.

isAllDigit

public static boolean isAllDigit(String s)

Checks if a given string contains only digits.

Parameters:
  • s – a string to be checked.
Returns:

true if the given string contains only digits, false otherwise.

isEmptyString

public static boolean isEmptyString(String s)

Checks if a given string is null or empty after trimmed.

Parameters:
  • s – the string to be checked.
Returns:

true if the given string is null or empty after trimmed, false otherwise.

isWrappedWith

public static boolean isWrappedWith(String s, String prefix, String suffix)

Checks if the given string is wrapped with the specified prefix and suffix.

Parameters:
  • s – the string to be checked.
  • prefix – the prefix.
  • suffix – the suffix.
Returns:

true if the given string is wrapped with the prefix and suffix.

parseBoolean

public static boolean parseBoolean(String s)

Parses the given string and returns the boolean value it represents.

Parameters:
  • s – the string to be parsed.
Returns:

true if and only if the given string equals, ignoring case, “true”.

parseBoolean

public static boolean parseBoolean(String s, boolean def)

Parses the given string and returns the boolean value it represents.

Parameters:
  • s – the string to be parsed.
  • def – the default boolean value when s is null.
Returns:

true if and only if the given string equals, ignoring case, “true”.

parseDouble

public static double parseDouble(String s)

Parses the given string and returns the double value it represents.

Parameters:
  • s – the string to be parsed.
Returns:

the double value the string represents or Double.NaN if unable to parse the string.

parseDouble

public static double parseDouble(String s, double def)

Parses the given string and returns the double value it represents.

Parameters:
  • s – the string to be parsed.
Returns:

the double value the string represents or the default value if unable to parse the string.

parseInt

public static int parseInt(String s)

Parses the given string and returns the integer value it represents.

Parameters:
  • s – the string to be parsed.
Returns:

the integer value the string represents or Integer.MIN_VALUE if unable to parse the string.

parseInt

public static int parseInt(String s, int def)

Parses the given string and returns the integer value it represents.

Parameters:
  • s – the string to be parsed.
Returns:

the integer value the string represents or the default value if unable to parse the string.

parseLong

public static long parseLong(String s)

Parses the given string and returns the long value it represents.

Parameters:
  • s – the string to be parsed.
Returns:

the long value the string represents or Long.MIN_VALUE if unable to parse the string.

parseLong

public static long parseLong(String s, long def)

Parses the given string and returns the long value it represents.

Parameters:
  • s – the string to be parsed.
Returns:

the long value the string represents or the default value if unable to parse the string.

removeLeadingCharacter

public static String removeLeadingCharacter(String s, char c)

Removes specified leading characters from the given string, if any.

Parameters:
  • s – the source string.
  • c – the leading character(s) to be removed.
Returns:

the string after removing the specified leading character(s).

removeLeadingSpace

public static String removeLeadingSpace(String s)

Removes leading spaces from the given string, if any.

Parameters:
  • s – the source string.
Returns:

the string after removing leading spaces.

removeLeadingZero

public static String removeLeadingZero(String s)

Removes leading zeros from the given string, if any.

Parameters:
  • s – the source string.
Returns:

the string after removing leading zeros.

repeat

public static String repeat(String s, int occurs)

Repeats a given string in the specified number of times, then concatenates and returns it.

Parameters:
  • s – a string to be repeated and concatenated.
  • occurs – the number of times of the given string to be repeated.
Returns:

the concatenated string.

toArray

public static String[] toArray(String s, String delim, int size)

Converts the tokenized string into a string array.

Parameters:
  • s – the string to be tokenized.
  • delim – the delimiters.
  • size – the size of the converted array.
Returns:

the string array.

toArray

public static String[] toArray(String s, int occurs)

Converts a given string into a string array.

Parameters:
  • s – the string to be converted.
  • occurs – the number of occurrences.
Returns:

the string array.

toArray

public static String[] toArray(String s)

Converts a given string into a string array. The resulted array will contain only one element, which is the specified string parameter.

Parameters:
  • s – the string to be converted.
Returns:

the string array.

toCharArray

public static char[] toCharArray(String s)

Converts the given string into an array of characters.

Parameters:
  • s – the string to be converted.
Returns:

an array of characters representing the given string or an empty array if the given string is null or empty.

toGMTString

public static String toGMTString(Date d)

Returns a string representation of the given Date object of the form: d MMM yyyy hh:mm:ss GMT

Parameters:
  • d – the date.
Returns:

the GMT string representation of the given date.

toHexString

public static String toHexString(byte[] b)

To Convert the byte array to the String in Hexdecimal format

Parameters:
  • b – byte array to convert
Throws:
Returns:

String in hexdecimal representation

toString

public static String toString(Throwable e)

Returns a string representation of the given throwable object, empty string if it is null. The resulted string contains a cause trace of the given throwable object.

Parameters:
  • e – the throwable object for getting its string representation.
Returns:

a string represenation of the given Object.

toString

public static String toString(Object obj)

Returns a string representation of the given object, empty string if it is null.

Parameters:
  • obj – the object for getting its string representation.
Returns:

a string represenation of the given Object.

tokenize

public static String[] tokenize(String str, String delim)

Tokenizes a given string according to the specified delimiters. The characters in the delim argument are the delimiters for separating tokens. Delimiter characters themselves will not be treated as tokens.

Parameters:
  • str – a string to be parsed.
  • delim – the delimiters.
Returns:

the tokens in a string array.

tokenize

public static String[] tokenize(String str, int fixedLength)

Tokenizes a given string according to a fixed length. If the last token’s length is less than the fixed length specified, it will be ignored.

Parameters:
  • str – a string to be parsed.
  • fixedLength – the fixed length.
Returns:

the tokens in a string array.

trim

public static String trim(String s)

Trims a given string. An empty string will be returned if the given string is null.

Parameters:
  • s – the string to be trimmed.
Returns:

the trimmed string.

trim

public static String trim(String s, String prefix, String suffix)

Trims the given string. If the given string starts with the specified prefix and ends with the specified suffix, both the prefix and the suffix will be trimmed out.

Parameters:
  • s – the string to be trimmed.
  • prefix – the prefix.
  • suffix – the suffix.
Returns:

the trimmed string.

trimAndVerifySize

public static String trimAndVerifySize(String s, int size)

Trims a given string and then verifies its size against the specified size. If the sizes do not match, null will be returned.

Parameters:
  • s – the string to be trimmed and verified.
  • size – the size for the verification.
Returns:

the trimmed string or null if the size verification failed.

wraps

public static String wraps(String s, String prefix, String suffix)

Wraps a given string with the specified prefix and suffix, if it is not alreay wrapped.

Parameters:
  • s – the string to be wrapped.
  • prefix – the prefix.
  • suffix – the suffix.
Returns:

the wrapped string.