com.googlecode.flyway.core.util
Class StringUtils

java.lang.Object
  extended by com.googlecode.flyway.core.util.StringUtils

public class StringUtils
extends java.lang.Object

Various string-related utilities.


Method Summary
static java.lang.String arrayToCommaDelimitedString(java.lang.String[] strings)
          Turns this string array in one comma-delimited string.
static java.lang.String collapseWhitespace(java.lang.String str)
          Replaces all sequences of whitespace by a single blank.
static java.lang.String collectionToCommaDelimitedString(java.util.Collection<?> collection)
          Convenience method to return a Collection as a comma-delimited String.
static int countOccurrencesOf(java.lang.String str, java.lang.String token)
          Counts the number of occurances of this token in this string.
static boolean hasLength(java.lang.String str)
          Checks whether this string is not null and not blank.
static boolean hasText(java.lang.String s)
          Checks whether this string contains at least one non-blank character.
static boolean isNumeric(java.lang.String str)
          Checks if the String contains only unicode digits.
static java.lang.String left(java.lang.String str, int count)
          Returns the first n characters from this string, where n = count.
static java.lang.String replace(java.lang.String inString, java.lang.String oldPattern, java.lang.String newPattern)
          Replace all occurences of a substring within a string with another string.
static java.lang.String replaceAll(java.lang.String str, java.lang.String originalToken, java.lang.String replacementToken)
          Replaces all occurrances of this originalToken in this string with this replacementToken.
static java.lang.String[] tokenizeToStringArray(java.lang.String str, java.lang.String delimiters)
          Splits this string into an array using these delimiters.
static java.lang.String trimLeadingWhitespace(java.lang.String str)
          Trim leading whitespace from the given String.
static java.lang.String trimOrPad(java.lang.String str, int length)
          Trims or pads (with spaces) this string, so it has this exact length.
static java.lang.String trimTrailingWhitespace(java.lang.String str)
          Trim trailing whitespace from the given String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

trimOrPad

public static java.lang.String trimOrPad(java.lang.String str,
                                         int length)
Trims or pads (with spaces) this string, so it has this exact length.

Parameters:
str - The string to adjust. null is treated as an empty string.
length - The exact length to reach.
Returns:
The adjusted string.

isNumeric

public static boolean isNumeric(java.lang.String str)

Checks if the String contains only unicode digits. A decimal point is not a unicode digit and returns false.

null will return false. An empty String ("") will return true.

 StringUtils.isNumeric(null)   = false
 StringUtils.isNumeric("")     = true
 StringUtils.isNumeric("  ")   = false
 StringUtils.isNumeric("123")  = true
 StringUtils.isNumeric("12 3") = false
 StringUtils.isNumeric("ab2c") = false
 StringUtils.isNumeric("12-3") = false
 StringUtils.isNumeric("12.3") = false
 

Parameters:
str - the String to check, may be null
Returns:
true if only contains digits, and is non-null

collapseWhitespace

public static java.lang.String collapseWhitespace(java.lang.String str)
Replaces all sequences of whitespace by a single blank. Ex.: "    " -> " "

Parameters:
str - The string to analyse.
Returns:
The input string, with all whitespace collapsed.

left

public static java.lang.String left(java.lang.String str,
                                    int count)
Returns the first n characters from this string, where n = count. If the string is shorter, the entire string will be returned. If the string is longer, it will be truncated.

Parameters:
str - The string to parse.
count - The amount of characters to return.
Returns:
The first n characters from this string, where n = count.

replaceAll

public static java.lang.String replaceAll(java.lang.String str,
                                          java.lang.String originalToken,
                                          java.lang.String replacementToken)
Replaces all occurrances of this originalToken in this string with this replacementToken.

Parameters:
str - The string to process.
originalToken - The token to replace.
replacementToken - The replacement.
Returns:
The transformed str.

hasLength

public static boolean hasLength(java.lang.String str)
Checks whether this string is not null and not blank.

Parameters:
str - The string to check.
Returns:
true if it has content, false if it is null or blank.

arrayToCommaDelimitedString

public static java.lang.String arrayToCommaDelimitedString(java.lang.String[] strings)
Turns this string array in one comma-delimited string.

Parameters:
strings - The array to process.
Returns:
The new comma-delimited string.

hasText

public static boolean hasText(java.lang.String s)
Checks whether this string contains at least one non-blank character.

Parameters:
s - The string to check.
Returns:
true if it has text, false if not.

tokenizeToStringArray

public static java.lang.String[] tokenizeToStringArray(java.lang.String str,
                                                       java.lang.String delimiters)
Splits this string into an array using these delimiters.

Parameters:
str - The string to split.
delimiters - The delimiters to use.
Returns:
The resulting array.

countOccurrencesOf

public static int countOccurrencesOf(java.lang.String str,
                                     java.lang.String token)
Counts the number of occurances of this token in this string.

Parameters:
str - The string to analyse.
token - The token to look for.
Returns:
The number of occurances.

replace

public static java.lang.String replace(java.lang.String inString,
                                       java.lang.String oldPattern,
                                       java.lang.String newPattern)
Replace all occurences of a substring within a string with another string.

Parameters:
inString - String to examine
oldPattern - String to replace
newPattern - String to insert
Returns:
a String with the replacements

collectionToCommaDelimitedString

public static java.lang.String collectionToCommaDelimitedString(java.util.Collection<?> collection)
Convenience method to return a Collection as a comma-delimited String. E.g. useful for toString() implementations.

Parameters:
collection - the Collection to analyse
Returns:
The comma-delimited String.

trimLeadingWhitespace

public static java.lang.String trimLeadingWhitespace(java.lang.String str)
Trim leading whitespace from the given String.

Parameters:
str - the String to check
Returns:
the trimmed String
See Also:
Character.isWhitespace(char)

trimTrailingWhitespace

public static java.lang.String trimTrailingWhitespace(java.lang.String str)
Trim trailing whitespace from the given String.

Parameters:
str - the String to check
Returns:
the trimmed String
See Also:
Character.isWhitespace(char)


Copyright © 2012. All Rights Reserved.