public class StringUtil extends Object
修飾子とタイプ | フィールドと説明 |
---|---|
static String |
EMPTY_STRING
空文字列の定数値。
|
static String[] |
EMPTY_STRING_ARRAY
An empty immutable
String array. |
static int |
INDEX_NOT_FOUND
Represents a failed index search.
|
修飾子とタイプ | メソッドと説明 |
---|---|
static String |
camelCase(String str)
文字列をキャメルケースに変換します。
|
static String |
camelCase(String str,
char separatorChar) |
static String |
capitalize(String str)
Capitalizes a String changing the first letter to title case as per
Character.toTitleCase(char) . |
static String |
chomp(String str)
Removes one newline from end of a String if it's there, otherwise leave it alone.
|
static String |
chop(String str)
Remove the last character from a String.
|
static boolean |
containsAny(CharSequence cs,
char... searchChars)
Checks if the CharSequence contains any character in the given set of characters.
|
static boolean |
containsAny(CharSequence cs,
CharSequence searchChars)
Checks if the CharSequence contains any character in the given set of characters.
|
static boolean |
containsNone(CharSequence cs,
char... searchChars)
Checks that the CharSequence does not contain certain characters.
|
static boolean |
containsNone(CharSequence cs,
String invalidChars)
Checks that the CharSequence does not contain certain characters.
|
static String |
decodeURI(String str)
JavaScript のグローバル関数である decodeURI を実装します。
|
static String |
decodeURIComponent(String str)
JavaScript のグローバル関数である decodeURIComponent を実装します。
|
static String |
encodeURI(String str)
JavaScript のグローバル関数である encodeURI を実装します。
|
static String |
encodeURIComponent(String str)
JavaScript のグローバル関数である encodeURIComponent を実装します。
|
static boolean |
equals(CharSequence cs1,
CharSequence cs2)
Compares two CharSequences, returning
true if they are equal. |
static int |
indexOfIgnoreCase(CharSequence str,
CharSequence searchStr)
Case in-sensitive find of the first index within a CharSequence.
|
static int |
indexOfIgnoreCase(CharSequence str,
CharSequence searchStr,
int startPos)
Case in-sensitive find of the first index within a CharSequence from the specified position.
|
static boolean |
isBlank(CharSequence cs)
Checks if a CharSequence is whitespace, empty ("") or null.
|
static boolean |
isEmpty(CharSequence cs)
Checks if a CharSequence is empty ("") or null.
|
static String |
join(Collection collection,
String delm)
コレクションの連結。
|
static String |
join(Object[] objects,
String delm)
オブジェクト配列の連結。
|
static double |
jsParseFloat(String s) |
static int |
jsParseInt(String s) |
static int |
jsParseInt(String s,
int radix) |
static int |
lastIndexOfIgnoreCase(CharSequence str,
CharSequence searchStr)
Case in-sensitive find of the last index within a CharSequence.
|
static int |
lastIndexOfIgnoreCase(CharSequence str,
CharSequence searchStr,
int startPos)
Case in-sensitive find of the last index within a CharSequence from the specified position.
|
static String |
left(String str,
int len)
Gets the leftmost
len characters of a String. |
static String |
leftPad(String str,
int size)
Left pad a String with spaces (' ').
|
static String |
leftPad(String str,
int size,
char padChar)
Left pad a String with a specified character.
|
static String |
leftPad(String str,
int size,
String padStr)
Left pad a String with a specified String.
|
static int |
length(CharSequence cs)
Gets a CharSequence length or
0 if the CharSequence is null . |
static String |
lowerCase(String str)
Converts a String to lower case as per
String.toLowerCase() . |
static String |
lowerCase(String str,
Locale locale)
Converts a String to lower case as per
String.toLowerCase(Locale) . |
static String |
remove(String str,
char remove)
Removes all occurrences of a character from within the source string.
|
static String |
remove(String str,
String remove)
Removes all occurrences of a substring from within the source string.
|
static String |
removeControlCharacter(String str,
char... exclusion)
UnicodeのC0制御文字(0x00-0x1F)、削除文字(0x7F)、および、C1制御文字(0x80-0x9F)を除去した文字列を返します。
|
static String |
removeControlCharacterWithoutTCL(String str)
タブ(0x09)、改行(0x0a)、復帰(0x0d)以外のUnicodeのC0制御文字(0x00-0x1F)、削除文字(0x7F)、および、C1制御文字(0x80-0x9F)を除去した文字列を返します。
|
static String |
removeEnd(String str,
String remove)
Removes a substring only if it is at the end of a source string, otherwise returns the source string.
|
static String |
removeStart(String str,
String remove)
Removes a substring only if it is at the beginning of a source string, otherwise returns the source string.
|
static String |
repeat(char ch,
int repeat)
Returns padding using the specified delimiter repeated to a given length.
|
static String |
repeat(String str,
int repeat)
Repeat a String
repeat times to form a new String. |
static String |
repeat(String str,
String separator,
int repeat)
Repeat a String
repeat times to form a new String, with a String separator injected each time. |
static String |
replace(String text,
String searchString,
String replacement)
Replaces all occurrences of a String within another String.
|
static String |
replace(String text,
String searchString,
String replacement,
int max)
Replaces a String with another String inside a larger String, for the first
max values of the search String. |
static String |
replaceChars(String str,
char searchChar,
char replaceChar)
Replaces all occurrences of a character in a String with another.
|
static String |
replaceChars(String str,
String searchChars,
String replaceChars)
Replaces multiple characters in a String in one go.
|
static String |
replaceOnce(String text,
String searchString,
String replacement)
Replaces a String with another String inside a larger String, once.
|
static String |
right(String str,
int len)
Gets the rightmost
len characters of a String. |
static String |
rightPad(String str,
int size)
Right pad a String with spaces (' ').
|
static String |
rightPad(String str,
int size,
char padChar)
Right pad a String with a specified character.
|
static String |
rightPad(String str,
int size,
String padStr)
Right pad a String with a specified 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[] |
splitByWholeSeparator(String str,
String separator)
Splits the provided text into an array, separator string specified.
|
static String[] |
splitByWholeSeparator(String str,
String separator,
int max)
Splits the provided text into an array, separator string specified.
|
static String |
substring(String str,
int start)
Gets a substring from the specified String avoiding exceptions.
|
static String |
substring(String str,
int start,
int end)
Gets a substring from the specified String avoiding exceptions.
|
static String |
swapCase(String str)
Swaps the case of a String changing upper and title case to lower case, and lower case to upper case.
|
static String |
trim(String str)
Removes control characters (char <= 32) from both ends of this String, handling
null by returning null
. |
static String |
uncapitalize(String str)
Uncapitalizes a String changing the first letter to title case as per
Character.toLowerCase(char) . |
static String |
upperCase(String str)
Converts a String to upper case as per
String.toUpperCase() . |
static String |
upperCase(String str,
Locale locale)
Converts a String to upper case as per
String.toUpperCase(Locale) . |
public static final int INDEX_NOT_FOUND
public static final String[] EMPTY_STRING_ARRAY
String
array.public static String join(Collection collection, String delm)
collection
- 連結を行うコレクションdelm
- 区切り文字public static String join(Object[] objects, String delm)
collection
- 連結を行うオブジェクト配列delm
- 区切り文字public static String camelCase(String str)
文字列をキャメルケースに変換します。
入力が null
の場合、 null
を返します。
StringUtil.camelCase(null) = null StringUtil.camelCase("_") = "" StringUtil.camelCase("aBc") = "aBc" StringUtil.camelCase("hoge_foo") = "hogeFoo" StringUtil.camelCase("HOGE_FOO") = "hOGEFOO" StringUtil.camelCase("hoge_foo_bar") = "hogeFooBar" StringUtil.camelCase("hoge__foo") = "hogeFoo" StringUtil.camelCase("hoge_") = "hoge" StringUtil.camelCase("_hoge_") = "hoge" StringUtil.camelCase("__hoge__") = "hoge"
str
- キャメルケースにする文字列(null でもよい)separatorChar
- デリミタとして使用する文字null
の場合、null
)public static boolean isEmpty(CharSequence cs)
Checks if a CharSequence is empty ("") or null.
StringUtil.isEmpty(null) = true StringUtil.isEmpty("") = true StringUtil.isEmpty(" ") = false StringUtil.isEmpty("bob") = false StringUtil.isEmpty(" bob ") = false
NOTE: This method changed in Lang version 2.0. It no longer trims the CharSequence. That functionality is available in isBlank().
cs
- the CharSequence to check, may be nulltrue
if the CharSequence is empty or nullpublic static boolean isBlank(CharSequence cs)
Checks if a CharSequence is whitespace, empty ("") or null.
StringUtil.isBlank(null) = true StringUtil.isBlank("") = true StringUtil.isBlank(" ") = true StringUtil.isBlank("bob") = false StringUtil.isBlank(" bob ") = false
cs
- the CharSequence to check, may be nulltrue
if the CharSequence is null, empty or whitespacepublic static String trim(String str)
Removes control characters (char <= 32) from both ends of this String, handling null
by returning null
.
The String is trimmed using String.trim()
. Trim removes start and end characters <= 32. To strip whitespace
use #strip(String)
.
To trim your choice of characters, use the #strip(String, String)
methods.
StringUtil.trim(null) = null StringUtil.trim("") = "" StringUtil.trim(" ") = "" StringUtil.trim("abc") = "abc" StringUtil.trim(" abc ") = "abc"
str
- the String to be trimmed, may be nullnull
if null String inputpublic static boolean containsNone(CharSequence cs, char... searchChars)
Checks that the CharSequence does not contain certain characters.
A null
CharSequence will return true
. A null
invalid character array will return true
. An
empty CharSequence (length()=0) always returns true.
StringUtils.containsNone(null, *) = true StringUtils.containsNone(*, null) = true StringUtils.containsNone("", *) = true StringUtils.containsNone("ab", '') = true StringUtils.containsNone("abab", 'xyz') = true StringUtils.containsNone("ab1", 'xyz') = true StringUtils.containsNone("abz", 'xyz') = false
cs
- the CharSequence to check, may be nullsearchChars
- an array of invalid chars, may be nullpublic static boolean containsNone(CharSequence cs, String invalidChars)
Checks that the CharSequence does not contain certain characters.
A null
CharSequence will return true
. A null
invalid character array will return true
. An
empty String ("") always returns true.
StringUtils.containsNone(null, *) = true StringUtils.containsNone(*, null) = true StringUtils.containsNone("", *) = true StringUtils.containsNone("ab", "") = true StringUtils.containsNone("abab", "xyz") = true StringUtils.containsNone("ab1", "xyz") = true StringUtils.containsNone("abz", "xyz") = false
cs
- the CharSequence to check, may be nullinvalidChars
- a String of invalid chars, may be nullpublic static boolean containsAny(CharSequence cs, char... searchChars)
Checks if the CharSequence contains any character in the given set of characters.
A null
CharSequence will return false
. A null
or zero length search array will return
false
.
StringUtils.containsAny(null, *) = false StringUtils.containsAny("", *) = false StringUtils.containsAny(*, null) = false StringUtils.containsAny(*, []) = false StringUtils.containsAny("zzabyycdxx",['z','a']) = true StringUtils.containsAny("zzabyycdxx",['b','y']) = true StringUtils.containsAny("aba", ['z']) = false
cs
- the CharSequence to check, may be nullsearchChars
- the chars to search for, may be nulltrue
if any of the chars are found, false
if no match or null inputpublic static boolean containsAny(CharSequence cs, CharSequence searchChars)
Checks if the CharSequence contains any character in the given set of characters.
A null
CharSequence will return false
. A null
search CharSequence will return false
.
StringUtils.containsAny(null, *) = false StringUtils.containsAny("", *) = false StringUtils.containsAny(*, null) = false StringUtils.containsAny(*, "") = false StringUtils.containsAny("zzabyycdxx", "za") = true StringUtils.containsAny("zzabyycdxx", "by") = true StringUtils.containsAny("aba","z") = false
cs
- the CharSequence to check, may be nullsearchChars
- the chars to search for, may be nulltrue
if any of the chars are found, false
if no match or null inputpublic static boolean equals(CharSequence cs1, CharSequence cs2)
Compares two CharSequences, returning true
if they are equal.
null
s are handled without exceptions. Two null
references are considered to be equal. The comparison is
case sensitive.
StringUtil.equals(null, null) = true StringUtil.equals(null, "abc") = false StringUtil.equals("abc", null) = false StringUtil.equals("abc", "abc") = true StringUtil.equals("abc", "ABC") = false
cs1
- the first CharSequence, may be nullcs2
- the second CharSequence, may be nulltrue
if the CharSequences are equal, case sensitive, or both null
String.equals(Object)
public static String substring(String str, int start)
Gets a substring from the specified String avoiding exceptions.
A negative start position can be used to start n
characters from the end of the String.
A null
String will return null
. An empty ("") String will return "".
StringUtil.substring(null, *) = null StringUtil.substring("", *) = "" StringUtil.substring("abc", 0) = "abc" StringUtil.substring("abc", 2) = "c" StringUtil.substring("abc", 4) = "" StringUtil.substring("abc", -2) = "bc" StringUtil.substring("abc", -4) = "abc"
str
- the String to get the substring from, may be nullstart
- the position to start from, negative means count back from the end of the String by this many charactersnull
if null String inputpublic static String substring(String str, int start, int end)
Gets a substring from the specified String avoiding exceptions.
A negative start position can be used to start/end n
characters from the end of the String.
The returned substring starts with the character in the start
position and ends before the end
position.
All position counting is zero-based -- i.e., to start at the beginning of the string use start = 0
. Negative
start and end positions can be used to specify offsets relative to the end of the String.
If start
is not strictly to the left of end
, "" is returned.
StringUtil.substring(null, *, *) = null StringUtil.substring("", * , *) = ""; StringUtil.substring("abc", 0, 2) = "ab" StringUtil.substring("abc", 2, 0) = "" StringUtil.substring("abc", 2, 4) = "c" StringUtil.substring("abc", 4, 6) = "" StringUtil.substring("abc", 2, 2) = "" StringUtil.substring("abc", -2, -1) = "b" StringUtil.substring("abc", -4, 2) = "ab"
str
- the String to get the substring from, may be nullstart
- the position to start from, negative means count back from the end of the String by this many charactersend
- the position to end at (exclusive), negative means count back from the end of the String by this many
charactersnull
if null String inputpublic static String left(String str, int len)
Gets the leftmost len
characters of a String.
If len
characters are not available, or the String is null
, the String will be returned without an
exception. An empty String is returned if len is negative.
StringUtil.left(null, *) = null StringUtil.left(*, -ve) = "" StringUtil.left("", *) = "" StringUtil.left("abc", 0) = "" StringUtil.left("abc", 2) = "ab" StringUtil.left("abc", 4) = "abc"
str
- the String to get the leftmost characters from, may be nulllen
- the length of the required Stringnull
if null String inputpublic static String right(String str, int len)
Gets the rightmost len
characters of a String.
If len
characters are not available, or the String is null
, the String will be returned without an an
exception. An empty String is returned if len is negative.
StringUtil.right(null, *) = null StringUtil.right(*, -ve) = "" StringUtil.right("", *) = "" StringUtil.right("abc", 0) = "" StringUtil.right("abc", 2) = "bc" StringUtil.right("abc", 4) = "abc"
str
- the String to get the rightmost characters from, may be nulllen
- the length of the required Stringnull
if null String inputpublic 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
.
StringUtil.split(null) = null StringUtil.split("") = [] StringUtil.split("abc def") = ["abc", "def"] StringUtil.split("abc def") = ["abc", "def"] StringUtil.split(" abc ") = ["abc"]
str
- the String to parse, may be nullnull
if null String inputpublic 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
.
StringUtil.split(null, *) = null StringUtil.split("", *) = [] StringUtil.split("a.b.c", '.') = ["a", "b", "c"] StringUtil.split("a..b.c", '.') = ["a", "b", "c"] StringUtil.split("a:b:c", '.') = ["a:b:c"] StringUtil.split("a b c", ' ') = ["a", "b", "c"]
str
- the String to parse, may be nullseparatorChar
- the character used as the delimiternull
if null String inputpublic 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.
StringUtil.split(null, *) = null StringUtil.split("", *) = [] StringUtil.split("abc def", null) = ["abc", "def"] StringUtil.split("abc def", " ") = ["abc", "def"] StringUtil.split("abc def", " ") = ["abc", "def"] StringUtil.split("ab:cd:ef", ":") = ["ab", "cd", "ef"]
str
- the String to parse, may be nullseparatorChars
- the characters used as the delimiters, null
splits on whitespacenull
if null String inputpublic 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).
StringUtil.split(null, *, *) = null StringUtil.split("", *, *) = [] StringUtil.split("ab de fg", null, 0) = ["ab", "cd", "ef"] StringUtil.split("ab de fg", null, 0) = ["ab", "cd", "ef"] StringUtil.split("ab:cd:ef", ":", 0) = ["ab", "cd", "ef"] StringUtil.split("ab:cd:ef", ":", 2) = ["ab", "cd:ef"]
str
- the String to parse, may be nullseparatorChars
- the characters used as the delimiters, null
splits on whitespacemax
- the maximum number of elements to include in the array. A zero or negative value implies no limitnull
if null String inputpublic static String[] splitByWholeSeparator(String str, String separator)
Splits the provided text into an array, separator string specified.
The separator(s) will not be included in the returned String array. Adjacent separators are treated as one separator.
A null
input String returns null
. A null
separator splits on whitespace.
StringUtil.splitByWholeSeparator(null, *) = null StringUtil.splitByWholeSeparator("", *) = [] StringUtil.splitByWholeSeparator("ab de fg", null) = ["ab", "de", "fg"] StringUtil.splitByWholeSeparator("ab de fg", null) = ["ab", "de", "fg"] StringUtil.splitByWholeSeparator("ab:cd:ef", ":") = ["ab", "cd", "ef"] StringUtil.splitByWholeSeparator("ab-!-cd-!-ef", "-!-") = ["ab", "cd", "ef"]
str
- the String to parse, may be nullseparator
- String containing the String to be used as a delimiter, null
splits on whitespacenull
if null String was inputpublic static String[] splitByWholeSeparator(String str, String separator, int max)
Splits the provided text into an array, separator string specified. Returns a maximum of max
substrings.
The separator(s) will not be included in the returned String array. Adjacent separators are treated as one separator.
A null
input String returns null
. A null
separator splits on whitespace.
StringUtil.splitByWholeSeparator(null, *, *) = null StringUtil.splitByWholeSeparator("", *, *) = [] StringUtil.splitByWholeSeparator("ab de fg", null, 0) = ["ab", "de", "fg"] StringUtil.splitByWholeSeparator("ab de fg", null, 0) = ["ab", "de", "fg"] StringUtil.splitByWholeSeparator("ab:cd:ef", ":", 2) = ["ab", "cd:ef"] StringUtil.splitByWholeSeparator("ab-!-cd-!-ef", "-!-", 5) = ["ab", "cd", "ef"] StringUtil.splitByWholeSeparator("ab-!-cd-!-ef", "-!-", 2) = ["ab", "cd-!-ef"]
str
- the String to parse, may be nullseparator
- String containing the String to be used as a delimiter, null
splits on whitespacemax
- the maximum number of elements to include in the returned array. A zero or negative value implies no limit.null
if null String was inputpublic static String removeStart(String str, String remove)
Removes a substring only if it is at the beginning of a source string, otherwise returns the source string.
A null
source string will return null
. An empty ("") source string will return the empty string. A
null
search string will return the source string.
StringUtil.removeStart(null, *) = null StringUtil.removeStart("", *) = "" StringUtil.removeStart(*, null) = * StringUtil.removeStart("www.domain.com", "www.") = "domain.com" StringUtil.removeStart("domain.com", "www.") = "domain.com" StringUtil.removeStart("www.domain.com", "domain") = "www.domain.com" StringUtil.removeStart("abc", "") = "abc"
str
- the source String to search, may be nullremove
- the String to search for and remove, may be nullnull
if null String inputpublic static String removeEnd(String str, String remove)
Removes a substring only if it is at the end of a source string, otherwise returns the source string.
A null
source string will return null
. An empty ("") source string will return the empty string. A
null
search string will return the source string.
StringUtil.removeEnd(null, *) = null StringUtil.removeEnd("", *) = "" StringUtil.removeEnd(*, null) = * StringUtil.removeEnd("www.domain.com", ".com.") = "www.domain.com" StringUtil.removeEnd("www.domain.com", ".com") = "www.domain" StringUtil.removeEnd("www.domain.com", "domain") = "www.domain.com" StringUtil.removeEnd("abc", "") = "abc"
str
- the source String to search, may be nullremove
- the String to search for and remove, may be nullnull
if null String inputpublic static String remove(String str, String remove)
Removes all occurrences of a substring from within the source string.
A null
source string will return null
. An empty ("") source string will return the empty string. A
null
remove string will return the source string. An empty ("") remove string will return the source string.
StringUtil.remove(null, *) = null StringUtil.remove("", *) = "" StringUtil.remove(*, null) = * StringUtil.remove(*, "") = * StringUtil.remove("queued", "ue") = "qd" StringUtil.remove("queued", "zz") = "queued"
str
- the source String to search, may be nullremove
- the String to search for and remove, may be nullnull
if null String inputpublic static String remove(String str, char remove)
Removes all occurrences of a character from within the source string.
A null
source string will return null
. An empty ("") source string will return the empty string.
StringUtil.remove(null, *) = null StringUtil.remove("", *) = "" StringUtil.remove("queued", 'u') = "qeed" StringUtil.remove("queued", 'z') = "queued"
str
- the source String to search, may be nullremove
- the char to search for and remove, may be nullnull
if null String inputpublic static String replaceOnce(String text, String searchString, String replacement)
Replaces a String with another String inside a larger String, once.
A null
reference passed to this method is a no-op.
StringUtil.replaceOnce(null, *, *) = null StringUtil.replaceOnce("", *, *) = "" StringUtil.replaceOnce("any", null, *) = "any" StringUtil.replaceOnce("any", *, null) = "any" StringUtil.replaceOnce("any", "", *) = "any" StringUtil.replaceOnce("aba", "a", null) = "aba" StringUtil.replaceOnce("aba", "a", "") = "ba" StringUtil.replaceOnce("aba", "a", "z") = "zba"
text
- text to search and replace in, may be nullsearchString
- the String to search for, may be nullreplacement
- the String to replace with, may be nullnull
if null String inputreplace(String text, String searchString, String replacement, int max)
public static String replace(String text, String searchString, String replacement)
Replaces all occurrences of a String within another String.
A null
reference passed to this method is a no-op.
StringUtil.replace(null, *, *) = null StringUtil.replace("", *, *) = "" StringUtil.replace("any", null, *) = "any" StringUtil.replace("any", *, null) = "any" StringUtil.replace("any", "", *) = "any" StringUtil.replace("aba", "a", null) = "aba" StringUtil.replace("aba", "a", "") = "b" StringUtil.replace("aba", "a", "z") = "zbz"
text
- text to search and replace in, may be nullsearchString
- the String to search for, may be nullreplacement
- the String to replace it with, may be nullnull
if null String inputreplace(String text, String searchString, String replacement, int max)
public static String replace(String text, String searchString, String replacement, 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.
StringUtil.replace(null, *, *, *) = null StringUtil.replace("", *, *, *) = "" StringUtil.replace("any", null, *, *) = "any" StringUtil.replace("any", *, null, *) = "any" StringUtil.replace("any", "", *, *) = "any" StringUtil.replace("any", *, *, 0) = "any" StringUtil.replace("abaa", "a", null, -1) = "abaa" StringUtil.replace("abaa", "a", "", -1) = "b" StringUtil.replace("abaa", "a", "z", 0) = "abaa" StringUtil.replace("abaa", "a", "z", 1) = "zbaa" StringUtil.replace("abaa", "a", "z", 2) = "zbza" StringUtil.replace("abaa", "a", "z", -1) = "zbzz"
text
- text to search and replace in, may be nullsearchString
- the String to search for, may be nullreplacement
- the String to replace it with, may be nullmax
- maximum number of values to replace, or -1
if no maximumnull
if null String inputpublic static String replaceChars(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.
StringUtil.replaceChars(null, *, *) = null StringUtil.replaceChars("", *, *) = "" StringUtil.replaceChars("abcba", 'b', 'y') = "aycya" StringUtil.replaceChars("abcba", 'z', 'y') = "abcba"
str
- String to replace characters in, may be nullsearchChar
- the character to search for, may be nullreplaceChar
- the character to replace, may be nullnull
if null string inputpublic static String replaceChars(String str, String searchChars, 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.
StringUtil.replaceChars(null, *, *) = null StringUtil.replaceChars("", *, *) = "" StringUtil.replaceChars("abc", null, *) = "abc" StringUtil.replaceChars("abc", "", *) = "abc" StringUtil.replaceChars("abc", "b", null) = "ac" StringUtil.replaceChars("abc", "b", "") = "ac" StringUtil.replaceChars("abcba", "bc", "yz") = "ayzya" StringUtil.replaceChars("abcba", "bc", "y") = "ayya" StringUtil.replaceChars("abcba", "bc", "yzx") = "ayzya"
str
- String to replace characters in, may be nullsearchChars
- a set of characters to search for, may be nullreplaceChars
- a set of characters to replace, may be nullnull
if null string inputpublic static String chomp(String str)
Removes one newline from end of a String if it's there, otherwise leave it alone. A newline is "\n
",
"\r
", or "\r\n
".
NOTE: This method changed in 2.0. It now more closely matches Perl chomp.
StringUtil.chomp(null) = null StringUtil.chomp("") = "" StringUtil.chomp("abc \r") = "abc " StringUtil.chomp("abc\n") = "abc" StringUtil.chomp("abc\r\n") = "abc" StringUtil.chomp("abc\r\n\r\n") = "abc\r\n" StringUtil.chomp("abc\n\r") = "abc\n" StringUtil.chomp("abc\n\rabc") = "abc\n\rabc" StringUtil.chomp("\r") = "" StringUtil.chomp("\n") = "" StringUtil.chomp("\r\n") = ""
str
- the String to chomp a newline from, may be nullnull
if null String inputpublic static String chop(String str)
Remove the last character from a String.
If the String ends in \r\n
, then remove both of them.
StringUtil.chop(null) = null StringUtil.chop("") = "" StringUtil.chop("abc \r") = "abc " StringUtil.chop("abc\n") = "abc" StringUtil.chop("abc\r\n") = "abc" StringUtil.chop("abc") = "ab" StringUtil.chop("abc\nabc") = "abc\nab" StringUtil.chop("a") = "" StringUtil.chop("\r") = "" StringUtil.chop("\n") = "" StringUtil.chop("\r\n") = ""
str
- the String to chop last character from, may be nullnull
if null String inputpublic static String repeat(String str, int repeat)
Repeat a String repeat
times to form a new String.
StringUtil.repeat(null, 2) = null StringUtil.repeat("", 0) = "" StringUtil.repeat("", 2) = "" StringUtil.repeat("a", 3) = "aaa" StringUtil.repeat("ab", 2) = "abab" StringUtil.repeat("a", -2) = ""
str
- the String to repeat, may be nullrepeat
- number of times to repeat str, negative treated as zeronull
if null String inputpublic static String repeat(String str, String separator, int repeat)
Repeat a String repeat
times to form a new String, with a String separator injected each time.
StringUtil.repeat(null, null, 2) = null StringUtil.repeat(null, "x", 2) = null StringUtil.repeat("", null, 0) = "" StringUtil.repeat("", "", 2) = "" StringUtil.repeat("", "x", 3) = "xxx" StringUtil.repeat("?", ", ", 3) = "?, ?, ?"
str
- the String to repeat, may be nullseparator
- the String to inject, may be nullrepeat
- number of times to repeat str, negative treated as zeronull
if null String inputpublic static String repeat(char ch, int repeat)
Returns padding using the specified delimiter repeated to a given length.
StringUtil.repeat(0, 'e') = "" StringUtil.repeat(3, 'e') = "eee" StringUtil.repeat(-2, 'e') = ""
Note: this method doesn't not support padding with Unicode Supplementary Characters as they require a
pair of char
s to be represented. If you are needing to support full I18N of your applications consider using
repeat(String, int)
instead.
ch
- character to repeatrepeat
- number of times to repeat char, negative treated as zerorepeat(String, int)
public static String rightPad(String str, int size)
Right pad a String with spaces (' ').
The String is padded to the size of size
.
StringUtil.rightPad(null, *) = null StringUtil.rightPad("", 3) = " " StringUtil.rightPad("bat", 3) = "bat" StringUtil.rightPad("bat", 5) = "bat " StringUtil.rightPad("bat", 1) = "bat" StringUtil.rightPad("bat", -1) = "bat"
str
- the String to pad out, may be nullsize
- the size to pad tonull
if null String inputpublic static String rightPad(String str, int size, char padChar)
Right pad a String with a specified character.
The String is padded to the size of size
.
StringUtil.rightPad(null, *, *) = null StringUtil.rightPad("", 3, 'z') = "zzz" StringUtil.rightPad("bat", 3, 'z') = "bat" StringUtil.rightPad("bat", 5, 'z') = "batzz" StringUtil.rightPad("bat", 1, 'z') = "bat" StringUtil.rightPad("bat", -1, 'z') = "bat"
str
- the String to pad out, may be nullsize
- the size to pad topadChar
- the character to pad withnull
if null String inputpublic static String rightPad(String str, int size, String padStr)
Right pad a String with a specified String.
The String is padded to the size of size
.
StringUtil.rightPad(null, *, *) = null StringUtil.rightPad("", 3, "z") = "zzz" StringUtil.rightPad("bat", 3, "yz") = "bat" StringUtil.rightPad("bat", 5, "yz") = "batyz" StringUtil.rightPad("bat", 8, "yz") = "batyzyzy" StringUtil.rightPad("bat", 1, "yz") = "bat" StringUtil.rightPad("bat", -1, "yz") = "bat" StringUtil.rightPad("bat", 5, null) = "bat " StringUtil.rightPad("bat", 5, "") = "bat "
str
- the String to pad out, may be nullsize
- the size to pad topadStr
- the String to pad with, null or empty treated as single spacenull
if null String inputpublic static String leftPad(String str, int size)
Left pad a String with spaces (' ').
The String is padded to the size of size
.
StringUtil.leftPad(null, *) = null StringUtil.leftPad("", 3) = " " StringUtil.leftPad("bat", 3) = "bat" StringUtil.leftPad("bat", 5) = " bat" StringUtil.leftPad("bat", 1) = "bat" StringUtil.leftPad("bat", -1) = "bat"
str
- the String to pad out, may be nullsize
- the size to pad tonull
if null String inputpublic static String leftPad(String str, int size, char padChar)
Left pad a String with a specified character.
Pad to a size of size
.
StringUtil.leftPad(null, *, *) = null StringUtil.leftPad("", 3, 'z') = "zzz" StringUtil.leftPad("bat", 3, 'z') = "bat" StringUtil.leftPad("bat", 5, 'z') = "zzbat" StringUtil.leftPad("bat", 1, 'z') = "bat" StringUtil.leftPad("bat", -1, 'z') = "bat"
str
- the String to pad out, may be nullsize
- the size to pad topadChar
- the character to pad withnull
if null String inputpublic static String leftPad(String str, int size, String padStr)
Left pad a String with a specified String.
Pad to a size of size
.
StringUtil.leftPad(null, *, *) = null StringUtil.leftPad("", 3, "z") = "zzz" StringUtil.leftPad("bat", 3, "yz") = "bat" StringUtil.leftPad("bat", 5, "yz") = "yzbat" StringUtil.leftPad("bat", 8, "yz") = "yzyzybat" StringUtil.leftPad("bat", 1, "yz") = "bat" StringUtil.leftPad("bat", -1, "yz") = "bat" StringUtil.leftPad("bat", 5, null) = " bat" StringUtil.leftPad("bat", 5, "") = " bat"
str
- the String to pad out, may be nullsize
- the size to pad topadStr
- the String to pad with, null or empty treated as single spacenull
if null String inputpublic static int length(CharSequence cs)
0
if the CharSequence is null
.cs
- a CharSequence or null
0
if the CharSequence is null
.public static String upperCase(String str)
Converts a String to upper case as per String.toUpperCase()
.
A null
input String returns null
.
StringUtil.upperCase(null) = null StringUtil.upperCase("") = "" StringUtil.upperCase("aBc") = "ABC"
Note: As described in the documentation for String.toUpperCase()
, the result of this method is
affected by the current locale. For platform-independent case transformations, the method
lowerCase(String, Locale)
should be used with a specific locale (e.g. Locale.ENGLISH
).
str
- the String to upper case, may be nullnull
if null String inputpublic static String upperCase(String str, Locale locale)
Converts a String to upper case as per String.toUpperCase(Locale)
.
A null
input String returns null
.
StringUtil.upperCase(null, Locale.ENGLISH) = null StringUtil.upperCase("", Locale.ENGLISH) = "" StringUtil.upperCase("aBc", Locale.ENGLISH) = "ABC"
str
- the String to upper case, may be nulllocale
- the locale that defines the case transformation rules, must not be nullnull
if null String inputpublic static String lowerCase(String str)
Converts a String to lower case as per String.toLowerCase()
.
A null
input String returns null
.
StringUtil.lowerCase(null) = null StringUtil.lowerCase("") = "" StringUtil.lowerCase("aBc") = "abc"
Note: As described in the documentation for String.toLowerCase()
, the result of this method is
affected by the current locale. For platform-independent case transformations, the method
lowerCase(String, Locale)
should be used with a specific locale (e.g. Locale.ENGLISH
).
str
- the String to lower case, may be nullnull
if null String inputpublic static String lowerCase(String str, Locale locale)
Converts a String to lower case as per String.toLowerCase(Locale)
.
A null
input String returns null
.
StringUtil.lowerCase(null, Locale.ENGLISH) = null StringUtil.lowerCase("", Locale.ENGLISH) = "" StringUtil.lowerCase("aBc", Locale.ENGLISH) = "abc"
str
- the String to lower case, may be nulllocale
- the locale that defines the case transformation rules, must not be nullnull
if null String inputpublic static String capitalize(String str)
Capitalizes a String changing the first letter to title case as per Character.toTitleCase(char)
. No other letters
are changed.
For a word based algorithm, see org.apache.commons.lang3.text.WordUtils#capitalize(String)
. A null
input
String returns null
.
StringUtil.capitalize(null) = null StringUtil.capitalize("") = "" StringUtil.capitalize("cat") = "Cat" StringUtil.capitalize("cAt") = "CAt"
str
- the String to capitalize, may be nullnull
if null String inputorg.apache.commons.lang3.text.WordUtils#capitalize(String)
,
uncapitalize(String)
public static String uncapitalize(String str)
Uncapitalizes a String changing the first letter to title case as per Character.toLowerCase(char)
. No other
letters are changed.
For a word based algorithm, see org.apache.commons.lang3.text.WordUtils#uncapitalize(String)
. A null
input String returns null
.
StringUtil.uncapitalize(null) = null StringUtil.uncapitalize("") = "" StringUtil.uncapitalize("Cat") = "cat" StringUtil.uncapitalize("CAT") = "cAT"
str
- the String to uncapitalize, may be nullnull
if null String inputorg.apache.commons.lang3.text.WordUtils#uncapitalize(String)
,
capitalize(String)
public static String swapCase(String str)
Swaps the case of a String changing upper and title case to lower case, and lower case to upper case.
For a word based algorithm, see org.apache.commons.lang3.text.WordUtils#swapCase(String)
. A null
input
String returns null
.
StringUtil.swapCase(null) = null StringUtil.swapCase("") = "" StringUtil.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"
NOTE: This method changed in Lang version 2.0. It no longer performs a word based algorithm. If you only use ASCII, you will notice no change. That functionality is available in org.apache.commons.lang3.text.WordUtils.
str
- the String to swap case, may be nullnull
if null String inputpublic static int indexOfIgnoreCase(CharSequence str, CharSequence searchStr)
Case in-sensitive find of the first index within a CharSequence.
A null
CharSequence will return -1
. A negative start position is treated as zero. An empty ("") search
CharSequence always matches. A start position greater than the string length only matches an empty search CharSequence.
StringUtil.indexOfIgnoreCase(null, *) = -1 StringUtil.indexOfIgnoreCase(*, null) = -1 StringUtil.indexOfIgnoreCase("", "") = 0 StringUtil.indexOfIgnoreCase("aabaabaa", "a") = 0 StringUtil.indexOfIgnoreCase("aabaabaa", "b") = 2 StringUtil.indexOfIgnoreCase("aabaabaa", "ab") = 1
str
- the CharSequence to check, may be nullsearchStr
- the CharSequence to find, may be nullnull
string inputpublic static int indexOfIgnoreCase(CharSequence str, CharSequence searchStr, int startPos)
Case in-sensitive find of the first index within a CharSequence from the specified position.
A null
CharSequence will return -1
. A negative start position is treated as zero. An empty ("") search
CharSequence always matches. A start position greater than the string length only matches an empty search CharSequence.
StringUtil.indexOfIgnoreCase(null, *, *) = -1 StringUtil.indexOfIgnoreCase(*, null, *) = -1 StringUtil.indexOfIgnoreCase("", "", 0) = 0 StringUtil.indexOfIgnoreCase("aabaabaa", "A", 0) = 0 StringUtil.indexOfIgnoreCase("aabaabaa", "B", 0) = 2 StringUtil.indexOfIgnoreCase("aabaabaa", "AB", 0) = 1 StringUtil.indexOfIgnoreCase("aabaabaa", "B", 3) = 5 StringUtil.indexOfIgnoreCase("aabaabaa", "B", 9) = -1 StringUtil.indexOfIgnoreCase("aabaabaa", "B", -1) = 2 StringUtil.indexOfIgnoreCase("aabaabaa", "", 2) = 2 StringUtil.indexOfIgnoreCase("abc", "", 9) = 3
str
- the CharSequence to check, may be nullsearchStr
- the CharSequence to find, may be nullstartPos
- the start position, negative treated as zeronull
string inputpublic static int lastIndexOfIgnoreCase(CharSequence str, CharSequence searchStr)
Case in-sensitive find of the last index within a CharSequence.
A null
CharSequence will return -1
. A negative start position returns -1
. An empty ("") search
CharSequence always matches unless the start position is negative. A start position greater than the string length
searches the whole string.
StringUtil.lastIndexOfIgnoreCase(null, *) = -1 StringUtil.lastIndexOfIgnoreCase(*, null) = -1 StringUtil.lastIndexOfIgnoreCase("aabaabaa", "A") = 7 StringUtil.lastIndexOfIgnoreCase("aabaabaa", "B") = 5 StringUtil.lastIndexOfIgnoreCase("aabaabaa", "AB") = 4
str
- the CharSequence to check, may be nullsearchStr
- the CharSequence to find, may be nullnull
string inputpublic static int lastIndexOfIgnoreCase(CharSequence str, CharSequence searchStr, int startPos)
Case in-sensitive find of the last index within a CharSequence from the specified position.
A null
CharSequence will return -1
. A negative start position returns -1
. An empty ("") search
CharSequence always matches unless the start position is negative. A start position greater than the string length
searches the whole string.
StringUtil.lastIndexOfIgnoreCase(null, *, *) = -1 StringUtil.lastIndexOfIgnoreCase(*, null, *) = -1 StringUtil.lastIndexOfIgnoreCase("aabaabaa", "A", 8) = 7 StringUtil.lastIndexOfIgnoreCase("aabaabaa", "B", 8) = 5 StringUtil.lastIndexOfIgnoreCase("aabaabaa", "AB", 8) = 4 StringUtil.lastIndexOfIgnoreCase("aabaabaa", "B", 9) = 5 StringUtil.lastIndexOfIgnoreCase("aabaabaa", "B", -1) = -1 StringUtil.lastIndexOfIgnoreCase("aabaabaa", "A", 0) = 0 StringUtil.lastIndexOfIgnoreCase("aabaabaa", "B", 0) = -1
str
- the CharSequence to check, may be nullsearchStr
- the CharSequence to find, may be nullstartPos
- the start positionnull
inputpublic static int jsParseInt(String s, int radix)
public static int jsParseInt(String s)
public static double jsParseFloat(String s)
public static String encodeURI(String str)
str
- 変換対象となる文字列public static String encodeURIComponent(String str)
str
- 変換対象となる文字列public static String decodeURI(String str)
str
- 変換対象となる文字列public static String decodeURIComponent(String str)
str
- 変換対象となる文字列public static String removeControlCharacter(String str, char... exclusion)
exclusion
で除去対象外とする制御文字を指定できます。exclusion
に制御文字以外の文字を指定した場合は無視されます。 str
- 制御文字を除去する文字列exclusion
- 除去対象外とする制御文字Copyright © 2012 NTT DATA INTRAMART CORPORATION