org.apache.fulcrum.yaafi.framework.util
Class StringUtils

java.lang.Object
  extended by org.apache.fulcrum.yaafi.framework.util.StringUtils

public class StringUtils
extends java.lang.Object

A subset of the utilities available in commons-lang-2.1 StringUtils.

Author:
Siegfried Goeschl

Constructor Summary
StringUtils()
           
 
Method Summary
static boolean isEmpty(java.lang.String str)
          Checks if a String is empty ("") or null.
static java.lang.String replace(java.lang.String text, java.lang.String repl, java.lang.String with)
          Replaces all occurrences of a String within another String.
static java.lang.String replace(java.lang.String text, java.lang.String repl, java.lang.String with, int max)
          Replaces a String with another String inside a larger String, for the first max values of the search String.
static java.lang.String replaceChars(java.lang.String str, char searchChar, char replaceChar)
          Replaces all occurrences of a character in a String with another.
static java.lang.String replaceChars(java.lang.String str, java.lang.String searchChars, java.lang.String replaceChars)
          Replaces multiple characters in a String in one go.
static java.lang.String replaceOnce(java.lang.String text, java.lang.String repl, java.lang.String with)
          Replaces a String with another String inside a larger String, once.
static java.lang.StringBuffer stringSubstitution(java.lang.String argStr, java.util.Map vars, boolean isLenient)
          Perform a series of substitutions.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringUtils

public StringUtils()
Method Detail

replaceOnce

public static java.lang.String replaceOnce(java.lang.String text,
                                           java.lang.String repl,
                                           java.lang.String with)

Replaces a String with another String inside a larger String, once.

A null reference passed to this method is a no-op.

 StringUtils.replaceOnce(null, *, *)        = null
 StringUtils.replaceOnce("", *, *)          = ""
 StringUtils.replaceOnce("aba", null, null) = "aba"
 StringUtils.replaceOnce("aba", null, null) = "aba"
 StringUtils.replaceOnce("aba", "a", null)  = "aba"
 StringUtils.replaceOnce("aba", "a", "")    = "aba"
 StringUtils.replaceOnce("aba", "a", "z")   = "zba"
 

Parameters:
text - text to search and replace in, may be null
repl - the String to search for, may be null
with - the String to replace with, may be null
Returns:
the text with any replacements processed, null if null String input
See Also:
replace(String text, String repl, String with, int max)

replace

public static java.lang.String replace(java.lang.String text,
                                       java.lang.String repl,
                                       java.lang.String with)

Replaces all occurrences of a String within another String.

A null reference passed to this method is a no-op.

 StringUtils.replace(null, *, *)        = null
 StringUtils.replace("", *, *)          = ""
 StringUtils.replace("aba", null, null) = "aba"
 StringUtils.replace("aba", null, null) = "aba"
 StringUtils.replace("aba", "a", null)  = "aba"
 StringUtils.replace("aba", "a", "")    = "aba"
 StringUtils.replace("aba", "a", "z")   = "zbz"
 

Parameters:
text - text to search and replace in, may be null
repl - the String to search for, may be null
with - the String to replace with, may be null
Returns:
the text with any replacements processed, null if null String input
See Also:
replace(String text, String repl, String with, int max)

replace

public static java.lang.String replace(java.lang.String text,
                                       java.lang.String repl,
                                       java.lang.String with,
                                       int max)

Replaces a String with another String inside a larger String, for the first max values of the search String.

A null reference passed to this method is a no-op.

 StringUtils.replace(null, *, *, *)         = null
 StringUtils.replace("", *, *, *)           = ""
 StringUtils.replace("abaa", null, null, 1) = "abaa"
 StringUtils.replace("abaa", null, null, 1) = "abaa"
 StringUtils.replace("abaa", "a", null, 1)  = "abaa"
 StringUtils.replace("abaa", "a", "", 1)    = "abaa"
 StringUtils.replace("abaa", "a", "z", 0)   = "abaa"
 StringUtils.replace("abaa", "a", "z", 1)   = "zbaa"
 StringUtils.replace("abaa", "a", "z", 2)   = "zbza"
 StringUtils.replace("abaa", "a", "z", -1)  = "zbzz"
 

Parameters:
text - text to search and replace in, may be null
repl - the String to search for, may be null
with - the String to replace with, may be null
max - maximum number of values to replace, or -1 if no maximum
Returns:
the text with any replacements processed, null if null String input

replaceChars

public static java.lang.String replaceChars(java.lang.String str,
                                            char searchChar,
                                            char replaceChar)

Replaces all occurrences of a character in a String with another. This is a null-safe version of String.replace(char, char).

A null string input returns null. An empty ("") string input returns an empty string.

 StringUtils.replaceChars(null, *, *)        = null
 StringUtils.replaceChars("", *, *)          = ""
 StringUtils.replaceChars("abcba", 'b', 'y') = "aycya"
 StringUtils.replaceChars("abcba", 'z', 'y') = "abcba"
 

Parameters:
str - String to replace characters in, may be null
searchChar - the character to search for, may be null
replaceChar - the character to replace, may be null
Returns:
modified String, null if null string input
Since:
2.0

replaceChars

public static java.lang.String replaceChars(java.lang.String str,
                                            java.lang.String searchChars,
                                            java.lang.String replaceChars)

Replaces multiple characters in a String in one go. This method can also be used to delete characters.

For example:
replaceChars("hello", "ho", "jy") = jelly.

A null string input returns null. An empty ("") string input returns an empty string. A null or empty set of search characters returns the input string.

The length of the search characters should normally equal the length of the replace characters. If the search characters is longer, then the extra search characters are deleted. If the search characters is shorter, then the extra replace characters are ignored.

 StringUtils.replaceChars(null, *, *)           = null
 StringUtils.replaceChars("", *, *)             = ""
 StringUtils.replaceChars("abc", null, *)       = "abc"
 StringUtils.replaceChars("abc", "", *)         = "abc"
 StringUtils.replaceChars("abc", "b", null)     = "ac"
 StringUtils.replaceChars("abc", "b", "")       = "ac"
 StringUtils.replaceChars("abcba", "bc", "yz")  = "ayzya"
 StringUtils.replaceChars("abcba", "bc", "y")   = "ayya"
 StringUtils.replaceChars("abcba", "bc", "yzx") = "ayzya"
 

Parameters:
str - String to replace characters in, may be null
searchChars - a set of characters to search for, may be null
replaceChars - a set of characters to replace, may be null
Returns:
modified String, null if null string input
Since:
2.0

isEmpty

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

Checks if a String is empty ("") or null.

 StringUtils.isEmpty(null)      = true
 StringUtils.isEmpty("")        = true
 StringUtils.isEmpty(" ")       = false
 StringUtils.isEmpty("bob")     = false
 StringUtils.isEmpty("  bob  ") = false
 

NOTE: This method changed in Lang version 2.0. It no longer trims the String. That functionality is available in isBlank().

Parameters:
str - the String to check, may be null
Returns:
true if the String is empty or null

stringSubstitution

public static java.lang.StringBuffer stringSubstitution(java.lang.String argStr,
                                                        java.util.Map vars,
                                                        boolean isLenient)
Perform a series of substitutions. The substitions are performed by replacing ${variable} in the target string with the value of provided by the key "variable" in the provided hashtable.

Parameters:
argStr - target string
vars - name/value pairs used for substitution
isLenient - ignore failures
Returns:
String target string with replacements.


Copyright © 2000-2011 Apache Software Foundation. All Rights Reserved.