站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > JBoss RULES 4.0.0.11754MR2 API 英文版文档

StringUtils (Drools 4.0.0.11754MR2 API) - JBoss RULES 4.0.0.11754MR2 API 英文版文档


org.drools.util
Class StringUtils

java.lang.Object
  extended by org.drools.util.StringUtils

public class StringUtils
extends Object

Ripped form commons StringUtil:

Operations on String that are null safe.

The StringUtils class defines certain words related to String handling.

StringUtils handles null input Strings quietly. That is to say that a null input will return null. Where a boolean or int is being returned details vary by method.

A side effect of the null handling is that a NullPointerException should be considered a bug in StringUtils (except for deprecated methods).

Methods in this class give sample code to explain their operation. The symbol * is used to indicate any input including null.

Since:
1.0
Version:
$Id$
Author:
Apache Jakarta Turbine, Jon S. Stevens, Daniel Rall, Greg Coladonato, Ed Korthof, Rand McNeely, Stephen Colebourne, Fredrik Westermarck, Holger Krauth, Alexander Day Chaffee, Henning P. Schmiedehausen, Arun Mammen Thomas, Gary Gregory, Phil Steitz, Al Chou, Michael Davey, Reuben Sivan, Chris Hyzer
See Also:
String

Field Summary
static String EMPTY
          The empty String "".
static String[] EMPTY_STRING_ARRAY
          An empty immutable String array.
static int INDEX_NOT_FOUND
          Represents a failed index search.
 
Constructor Summary
StringUtils()
          StringUtils instances should NOT be constructed in standard programming.
 
Method Summary
static boolean isEmpty(String str)
          Checks if a String is empty ("") or null.
static String padding(int repeat, char padChar)
          Returns padding using the specified delimiter repeated to a given length.
static String readFileAsString(Reader reader)
           
static String repeat(String str, int repeat)
          Repeat a String repeat times to form a new String.
static String[] split(String str)
          Splits the provided text into an array, using whitespace as the separator.
static String[] split(String str, char separatorChar)
          Splits the provided text into an array, separator specified.
static String[] split(String str, String separatorChars)
          Splits the provided text into an array, separators specified.
static String[] split(String str, String separatorChars, int max)
          Splits the provided text into an array with a maximum length, separators specified.
static String[] splitPreserveAllTokens(String str)
          Splits the provided text into an array, using whitespace as the separator, preserving all tokens, including empty tokens created by adjacent separators.
static String[] splitPreserveAllTokens(String str, char separatorChar)
          Splits the provided text into an array, separator specified, preserving all tokens, including empty tokens created by adjacent separators.
static String[] splitPreserveAllTokens(String str, String separatorChars)
          Splits the provided text into an array, separators specified, preserving all tokens, including empty tokens created by adjacent separators.
static String[] splitPreserveAllTokens(String str, String separatorChars, int max)
          Splits the provided text into an array with a maximum length, separators specified, preserving all tokens, including empty tokens created by adjacent separators.
static String ucFirst(String name)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_STRING_ARRAY

public static final String[] EMPTY_STRING_ARRAY
An empty immutable String array.


EMPTY

public static final String EMPTY
The empty String "".

Since:
2.0
See Also:
Constant Field Values

INDEX_NOT_FOUND

public static final int INDEX_NOT_FOUND
Represents a failed index search.

Since:
2.1
See Also:
Constant Field Values
Constructor Detail

StringUtils

public StringUtils()

StringUtils instances should NOT be constructed in standard programming. Instead, the class should be used as StringUtils.trim(" foo ");.

This constructor is public to permit tools that require a JavaBean instance to operate.

Method Detail

ucFirst

public static String ucFirst(String name)

isEmpty

public static boolean isEmpty(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

repeat

public static String repeat(String str,
                            int repeat)

Repeat a String repeat times to form a new String.

 StringUtils.repeat(null, 2) = null
 StringUtils.repeat("", 0)   = ""
 StringUtils.repeat("", 2)   = ""
 StringUtils.repeat("a", 3)  = "aaa"
 StringUtils.repeat("ab", 2) = "abab"
 StringUtils.repeat("a", -2) = ""
 

Parameters:
str - the String to repeat, may be null
repeat - number of times to repeat str, negative treated as zero
Returns:
a new String consisting of the original String repeated, null if null String input

split

public static String[] split(String str)

Splits the provided text into an array, using whitespace as the separator. Whitespace is defined by Character.isWhitespace(char).

The separator is not included in the returned String array. Adjacent separators are treated as one separator. For more control over the split use the StrTokenizer class.

A null input String returns null.

 StringUtils.split(null)       = null
 StringUtils.split("")         = []
 StringUtils.split("abc def")  = ["abc", "def"]
 StringUtils.split("abc  def") = ["abc", "def"]
 StringUtils.split(" abc ")    = ["abc"]
 

Parameters:
str - the String to parse, may be null
Returns:
an array of parsed Strings, null if null String input

split

public static String[] split(String str,
                             char separatorChar)

Splits the provided text into an array, separator specified. This is an alternative to using StringTokenizer.

The separator is not included in the returned String array. Adjacent separators are treated as one separator. For more control over the split use the StrTokenizer class.

A null input String returns null.

 StringUtils.split(null, *)         = null
 StringUtils.split("", *)           = []
 StringUtils.split("a.b.c", '.')    = ["a", "b", "c"]
 StringUtils.split("a..b.c", '.')   = ["a", "b", "c"]
 StringUtils.split("a:b:c", '.')    = ["a:b:c"]
 StringUtils.split("a\tb\nc", null) = ["a", "b", "c"]
 StringUtils.split("a b c", ' ')    = ["a", "b", "c"]
 

Parameters:
str - the String to parse, may be null
separatorChar - the character used as the delimiter, null splits on whitespace
Returns:
an array of parsed Strings, null if null String input
Since:
2.0

split

public static String[] split(String str,
                             String separatorChars)

Splits the provided text into an array, separators specified. This is an alternative to using StringTokenizer.

The separator is not included in the returned String array. Adjacent separators are treated as one separator. For more control over the split use the StrTokenizer class.

A null input String returns null. A null separatorChars splits on whitespace.

 StringUtils.split(null, *)         = null
 StringUtils.split("", *)           = []
 StringUtils.split("abc def", null) = ["abc", "def"]
 StringUtils.split("abc def", " ")  = ["abc", "def"]
 StringUtils.split("abc  def", " ") = ["abc", "def"]
 StringUtils.split("ab:cd:ef", ":") = ["ab", "cd", "ef"]
 

Parameters:
str - the String to parse, may be null
separatorChars - the characters used as the delimiters, null splits on whitespace
Returns:
an array of parsed Strings, null if null String input

split

public static String[] split(String str,
                             String separatorChars,
                             int max)

Splits the provided text into an array with a maximum length, separators specified.

The separator is not included in the returned String array. Adjacent separators are treated as one separator.

A null input String returns null. A null separatorChars splits on whitespace.

If more than max delimited substrings are found, the last returned string includes all characters after the first max - 1 returned strings (including separator characters).

 StringUtils.split(null, *, *)            = null
 StringUtils.split("", *, *)              = []
 StringUtils.split("ab de fg", null, 0)   = ["ab", "cd", "ef"]
 StringUtils.split("ab   de fg", null, 0) = ["ab", "cd", "ef"]
 StringUtils.split("ab:cd:ef", ":", 0)    = ["ab", "cd", "ef"]
 StringUtils.split("ab:cd:ef", ":", 2)    = ["ab", "cd:ef"]
 

Parameters:
str - the String to parse, may be null
separatorChars - the characters used as the delimiters, null splits on whitespace
max - the maximum number of elements to include in the array. A zero or negative value implies no limit
Returns:
an array of parsed Strings, null if null String input

splitPreserveAllTokens

public static String[] splitPreserveAllTokens(String str)

Splits the provided text into an array, using whitespace as the separator, preserving all tokens, including empty tokens created by adjacent separators. This is an alternative to using StringTokenizer. Whitespace is defined by Character.isWhitespace(char).

The separator is not included in the returned String array. Adjacent separators are treated as separators for empty tokens. For more control over the split use the StrTokenizer class.

A null input String returns null.

 StringUtils.splitPreserveAllTokens(null)       = null
 StringUtils.splitPreserveAllTokens("")         = []
 StringUtils.splitPreserveAllTokens("abc def")  = ["abc", "def"]
 StringUtils.splitPreserveAllTokens("abc  def") = ["abc", "", "def"]
 StringUtils.splitPreserveAllTokens(" abc ")    = ["", "abc", ""]
 

Parameters:
str - the String to parse, may be null
Returns:
an array of parsed Strings, null if null String input
Since:
2.1

splitPreserveAllTokens

public static String[] splitPreserveAllTokens(String str,
                                              char separatorChar)

Splits the provided text into an array, separator specified, preserving all tokens, including empty tokens created by adjacent separators. This is an alternative to using StringTokenizer.

The separator is not included in the returned String array. Adjacent separators are treated as separators for empty tokens. For more control over the split use the StrTokenizer class.

A null input String returns null.

 StringUtils.splitPreserveAllTokens(null, *)         = null
 StringUtils.splitPreserveAllTokens("", *)           = []
 StringUtils.splitPreserveAllTokens("a.b.c", '.')    = ["a", "b", "c"]
 StringUtils.splitPreserveAllTokens("a..b.c", '.')   = ["a", "", "b", "c"]
 StringUtils.splitPreserveAllTokens("a:b:c", '.')    = ["a:b:c"]
 StringUtils.splitPreserveAllTokens("a\tb\nc", null) = ["a", "b", "c"]
 StringUtils.splitPreserveAllTokens("a b c", ' ')    = ["a", "b", "c"]
 StringUtils.splitPreserveAllTokens("a b c ", ' ')   = ["a", "b", "c", ""]
 StringUtils.splitPreserveAllTokens("a b c  ", ' ')   = ["a", "b", "c", "", ""]
 StringUtils.splitPreserveAllTokens(" a b c", ' ')   = ["", a", "b", "c"]
 StringUtils.splitPreserveAllTokens("  a b c", ' ')  = ["", "", a", "b", "c"]
 StringUtils.splitPreserveAllTokens(" a b c ", ' ')  = ["", a", "b", "c", ""]
 

Parameters:
str - the String to parse, may be null
separatorChar - the character used as the delimiter, null splits on whitespace
Returns:
an array of parsed Strings, null if null String input
Since:
2.1

splitPreserveAllTokens

public static String[] splitPreserveAllTokens(String str,
                                              String separatorChars)

Splits the provided text into an array, separators specified, preserving all tokens, including empty tokens created by adjacent separators. This is an alternative to using StringTokenizer.

The separator is not included in the returned String array. Adjacent separators are treated as separators for empty tokens. For more control over the split use the StrTokenizer class.

A null input String returns null. A null separatorChars splits on whitespace.

 StringUtils.splitPreserveAllTokens(null, *)           = null
 StringUtils.splitPreserveAllTokens("", *)             = []
 StringUtils.splitPreserveAllTokens("abc def", null)   = ["abc", "def"]
 StringUtils.splitPreserveAllTokens("abc def", " ")    = ["abc", "def"]
 StringUtils.splitPreserveAllTokens("abc  def", " ")   = ["abc", "", def"]
 StringUtils.splitPreserveAllTokens("ab:cd:ef", ":")   = ["ab", "cd", "ef"]
 StringUtils.splitPreserveAllTokens("ab:cd:ef:", ":")  = ["ab", "cd", "ef", ""]
 StringUtils.splitPreserveAllTokens("ab:cd:ef::", ":") = ["ab", "cd", "ef", "", ""]
 StringUtils.splitPreserveAllTokens("ab::cd:ef", ":")  = ["ab", "", cd", "ef"]
 StringUtils.splitPreserveAllTokens(":cd:ef", ":")     = ["", cd", "ef"]
 StringUtils.splitPreserveAllTokens("::cd:ef", ":")    = ["", "", cd", "ef"]
 StringUtils.splitPreserveAllTokens(":cd:ef:", ":")    = ["", cd", "ef", ""]
 

Parameters:
str - the String to parse, may be null
separatorChars - the characters used as the delimiters, null splits on whitespace
Returns:
an array of parsed Strings, null if null String input
Since:
2.1

splitPreserveAllTokens

public static String[] splitPreserveAllTokens(String str,
                                              String separatorChars,
                                              int max)

Splits the provided text into an array with a maximum length, separators specified, preserving all tokens, including empty tokens created by adjacent separators.

The separator is not included in the returned String array. Adjacent separators are treated as separators for empty tokens. Adjacent separators are treated as one separator.

A null input String returns null. A null separatorChars splits on whitespace.

If more than max delimited substrings are found, the last returned string includes all characters after the first max - 1 returned strings (including separator characters).

 StringUtils.splitPreserveAllTokens(null, *, *)            = null
 StringUtils.splitPreserveAllTokens("", *, *)              = []
 StringUtils.splitPreserveAllTokens("ab de fg", null, 0)   = ["ab", "cd", "ef"]
 StringUtils.splitPreserveAllTokens("ab   de fg", null, 0) = ["ab", "cd", "ef"]
 StringUtils.splitPreserveAllTokens("ab:cd:ef", ":", 0)    = ["ab", "cd", "ef"]
 StringUtils.splitPreserveAllTokens("ab:cd:ef", ":", 2)    = ["ab", "cd:ef"]
 StringUtils.splitPreserveAllTokens("ab   de fg", null, 2) = ["ab", "  de fg"]
 StringUtils.splitPreserveAllTokens("ab   de fg", null, 3) = ["ab", "", " de fg"]
 StringUtils.splitPreserveAllTokens("ab   de fg", null, 4) = ["ab", "", "", "de fg"]
 

Parameters:
str - the String to parse, may be null
separatorChars - the characters used as the delimiters, null splits on whitespace
max - the maximum number of elements to include in the array. A zero or negative value implies no limit
Returns:
an array of parsed Strings, null if null String input
Since:
2.1

padding

public static String padding(int repeat,
                             char padChar)
                      throws IndexOutOfBoundsException

Returns padding using the specified delimiter repeated to a given length.

 StringUtils.padding(0, 'e')  = ""
 StringUtils.padding(3, 'e')  = "eee"
 StringUtils.padding(-2, 'e') = IndexOutOfBoundsException
 

Note: this method doesn't not support padding with Unicode Supplementary Characters as they require a pair of chars to be represented. If you are needing to support full I18N of your applications consider using repeat(String, int) instead.

Parameters:
repeat - number of times to repeat delim
padChar - character to repeat
Returns:
String with repeated character
Throws:
IndexOutOfBoundsException - if repeat < 0
See Also:
repeat(String, int)

readFileAsString

public static String readFileAsString(Reader reader)
Parameters:
filePath - the name of the file to open. Not sure if it can accept URLs or just filenames. Path handling could be better, and buffer sizes are hardcoded


Copyright © 2001-2007 JBoss Inc.. All Rights Reserved.