public class StringUtil extends Object
| 构造器和说明 |
|---|
StringUtil() |
| 限定符和类型 | 方法和说明 |
|---|---|
static String |
capitalize(String str)
Capitalizes a string, changing the first letter to upper case.
|
static boolean |
compare(String a,
String b)
Description:
判断两个字符串是否值相等 |
static boolean |
compareIgnoreCase(String a,
String b)
Description:
判断两个字符串是否值相等,忽略大小写 |
static String |
compressChars(String s,
char c)
Compress multiple occurrences of given char into one appearance.
|
static boolean |
containsOnlyDigits(String string)
Returns
true if string contains only digits. |
static boolean |
containsOnlyWhitespaces(String string)
Returns
true if string contains only white spaces. |
static String |
convertCharset(String source,
String srcCharsetName,
String newCharsetName)
Converts string charset.
|
static String |
copy(String src,
int len)
Description:
复制字符串中从开始到指定的位置 |
static int |
count(String source,
char c) |
static int |
count(String source,
char c,
int start) |
static int |
count(String source,
String sub)
Counts substring occurrences in a source string.
|
static int |
count(String source,
String sub,
int start) |
static int |
countIgnoreCase(String source,
String sub)
Count substring occurrences in a source string, ignoring case.
|
static String |
crop(String string)
Crops string by setting empty strings to
null. |
static void |
cropAll(String[] strings)
Crops all elements of string array.
|
static String |
cutFromIndexOf(String string,
char c)
Cuts the string from the first index of provided char to the end.
|
static String |
cutFromIndexOf(String string,
String substring)
Cuts the string from the first index of provided substring to the end.
|
static String |
cutPrefix(String string,
String prefix)
Cuts prefix if exists.
|
static String |
cutSuffix(String string,
String suffix)
Cuts sufix if exists.
|
static String |
cutSurrounding(String string,
String fix) |
static String |
cutSurrounding(String string,
String prefix,
String suffix)
Removes surrounding prefix and suffixes.
|
static String |
cutToIndexOf(String string,
char c)
Cuts the string from beginning to the first index of provided char.
|
static String |
cutToIndexOf(String string,
String substring)
Cuts the string from beginning to the first index of provided substring.
|
static boolean |
endsWithChar(String s,
char c)
Returns if string ends with provided character.
|
static boolean |
endsWithIgnoreCase(String src,
String subS)
Tests if this string ends with the specified suffix.
|
static int |
endsWithOne(String src,
String[] dest)
Checks if string ends with at least one string from the provided array.
|
static int |
endsWithOneIgnoreCase(String src,
String[] dest)
Checks if string ends with at least one string from the provided array.
|
static boolean |
equals(String[] as,
String[] as1)
Compares two string arrays.
|
static boolean |
equals(String s1,
String s2)
Compares 2 strings.
|
static boolean |
equalsIgnoreCase(String[] as,
String[] as1)
Compares two string arrays.
|
static int |
equalsOne(String src,
String[] dest)
Compares string with at least one from the provided array.
|
static int |
equalsOneIgnoreCase(String src,
String[] dest)
Compares string with at least one from the provided array, ignoring case.
|
static String |
escapeJava(String string)
Escapes a string using java rules.
|
static String |
findCommonPrefix(String... strings)
Finds common prefix for several strings.
|
static String |
fromCamelCase(String input,
char separator)
Changes CamelCase string to lower case words separated by provided
separator character.
|
static int |
indexOf(String src,
char c,
int startIndex,
int endIndex)
Finds the first occurrence of a character in the given source but within
limited range (start, end].
|
static int[] |
indexOf(String s,
String[] arr)
Finds the very first index of a substring from the specified array.
|
static int[] |
indexOf(String s,
String[] arr,
int start)
Finds the very first index of a substring from the specified array.
|
static int |
indexOf(String src,
String sub,
int startIndex,
int endIndex)
Finds first occurrence of a substring in the given source but within
limited range [start, end).
|
static int |
indexOfChars(String string,
char[] chars) |
static int |
indexOfChars(String string,
char[] chars,
int startindex)
Returns the very first index of any char from provided string, starting
from specified index offset.
|
static int |
indexOfChars(String string,
String chars) |
static int |
indexOfChars(String string,
String chars,
int startindex)
Returns the very first index of any char from provided string, starting
from specified index offset.
|
static int |
indexOfIgnoreCase(String src,
char c,
int startIndex,
int endIndex)
Finds the first occurrence of a character in the given source but within
limited range (start, end].
|
static int |
indexOfIgnoreCase(String src,
String subS)
Finds first index of a substring in the given source string with ignored
case.
|
static int[] |
indexOfIgnoreCase(String s,
String[] arr)
Finds the very first index of a substring from the specified array.
|
static int[] |
indexOfIgnoreCase(String s,
String[] arr,
int start)
Finds the very first index of a substring from the specified array.
|
static int |
indexOfIgnoreCase(String src,
String subS,
int startIndex)
Finds first index of a substring in the given source string with ignored
case.
|
static int |
indexOfIgnoreCase(String src,
String sub,
int startIndex,
int endIndex)
Finds first index of a substring in the given source string and range
with ignored case.
|
static int |
indexOfNonWhitespace(String string) |
static int |
indexOfNonWhitespace(String string,
int startindex) |
static int |
indexOfNonWhitespace(String string,
int startindex,
int endindex) |
static int[] |
indexOfRegion(String string,
String leftBoundary,
String rightBoundary) |
static int[] |
indexOfRegion(String string,
String leftBoundary,
String rightBoundary,
char escape) |
static int[] |
indexOfRegion(String string,
String leftBoundary,
String rightBoundary,
char escape,
int offset)
Returns indexes of the first string region.
|
static int[] |
indexOfRegion(String string,
String leftBoundary,
String rightBoundary,
int offset)
Returns indexes of the first region without escaping character.
|
static int |
indexOfUnescapedChar(String src,
char sub,
char escapeChar) |
static int |
indexOfUnescapedChar(String src,
char sub,
char escapeChar,
int startIndex) |
static int |
indexOfWhitespace(String string)
Returns first index of a whitespace character.
|
static int |
indexOfWhitespace(String string,
int startindex) |
static int |
indexOfWhitespace(String string,
int startindex,
int endindex)
Returns first index of a whitespace character, starting from specified
index offset.
|
static String |
insert(String src,
String insert) |
static String |
insert(String src,
String insert,
int offset)
Inserts a string on provided offset.
|
static boolean |
isAllBlank(String... strings)
Determines if string array contains just blank strings.
|
static boolean |
isAllEmpty(String... strings)
Determines if string array contains empty strings.
|
static boolean |
isBlank(String string)
Determines if a string is blank (
null or
containsOnlyWhitespaces(String)). |
static boolean |
isCharAtEqual(String string,
int index,
char charToCompare)
Safely compares provided char with char on given location.
|
static boolean |
isCharAtEscaped(String src,
int ndx,
char escapeChar)
Returns
true if character at provided index position is
escaped by escape character. |
static boolean |
isEmpty(String string)
Description:
判断字符串是否为空 Determines if a string is empty ( null or zero-length). |
static boolean |
isNotBlank(String string)
Determines if string is not blank.
|
static boolean |
isNotEmpty(String string)
Determines if a string is not empty.
|
static String |
join(Iterable<?> elements,
String separator)
Joins list of iterable elements.
|
static String |
join(String... parts)
Joins an array of strings into one string.
|
static int |
lastIndexOf(String src,
char c,
int startIndex,
int endIndex)
Finds last index of a character in the given source string in specified
range [end, start]
|
static int[] |
lastIndexOf(String s,
String[] arr)
Finds the very last index of a substring from the specified array.
|
static int[] |
lastIndexOf(String s,
String[] arr,
int fromIndex)
Finds the very last index of a substring from the specified array.
|
static int |
lastIndexOf(String src,
String sub,
int startIndex,
int endIndex)
Finds last index of a substring in the given source string in specified
range [end, start] See
indexOf(String, String, int, int) for
details about the speed. |
static int |
lastIndexOfIgnoreCase(String src,
char c,
int startIndex,
int endIndex)
Finds last index of a character in the given source string in specified
range [end, start]
|
static int |
lastIndexOfIgnoreCase(String s,
String subS)
Finds last index of a substring in the given source string with ignored
case.
|
static int[] |
lastIndexOfIgnoreCase(String s,
String[] arr)
Finds the very last index of a substring from the specified array.
|
static int[] |
lastIndexOfIgnoreCase(String s,
String[] arr,
int fromIndex)
Finds the very last index of a substring from the specified array.
|
static int |
lastIndexOfIgnoreCase(String src,
String subS,
int startIndex)
Finds last index of a substring in the given source string with ignored
case.
|
static int |
lastIndexOfIgnoreCase(String src,
String sub,
int startIndex,
int endIndex)
Finds last index of a substring in the given source string with ignored
case in specified range.
|
static int |
lastIndexOfNonWhitespace(String src) |
static int |
lastIndexOfNonWhitespace(String src,
int startIndex) |
static int |
lastIndexOfNonWhitespace(String src,
int startIndex,
int endIndex) |
static int |
lastIndexOfWhitespace(String src) |
static int |
lastIndexOfWhitespace(String src,
int startIndex)
Returns last index of a whitespace.
|
static int |
lastIndexOfWhitespace(String src,
int startIndex,
int endIndex)
Returns last index of a whitespace.
|
static String |
maxCommonPrefix(String one,
String two)
Returns max common prefix of two strings.
|
static String |
prefix(String string,
String prefix)
Inserts prefix if doesn't exist.
|
static String |
remove(String string,
char ch)
Removes a single character from string.
|
static String |
remove(String s,
String sub)
Removes all substring occurrences from the string.
|
static String |
removeChars(String src,
char... chars)
Removes set of characters from string.
|
static String |
removeChars(String src,
String chars)
Removes all characters contained in provided string.
|
static String |
repeat(char c,
int count) |
static String |
repeat(String source,
int count)
Creates a new string that contains the provided string a number of times.
|
static String |
replace(String s,
String[] sub,
String[] with)
Replaces many substring at once.
|
static String |
replace(String s,
String sub,
String with)
Replaces all occurrences of a certain pattern in a string with a
replacement string.
|
static String |
replaceChar(String s,
char sub,
char with)
Replaces all occurrences of a character in a string.
|
static String |
replaceChars(String s,
char[] sub,
char[] with)
Replaces all occurrences of a characters in a string.
|
static String |
replaceFirst(String s,
char sub,
char with)
Replaces the very first occurrence of a character in a string.
|
static String |
replaceFirst(String s,
String sub,
String with)
Replaces the very first occurrence of a substring with supplied string.
|
static String |
replaceIgnoreCase(String s,
String[] sub,
String[] with)
Replaces many substring at once.
|
static String |
replaceLast(String s,
char sub,
char with)
Replaces the very last occurrence of a character in a string.
|
static String |
replaceLast(String s,
String sub,
String with)
Replaces the very last occurrence of a substring with supplied string.
|
static String |
reverse(String s)
Reverse a string.
|
static String |
shorten(String s,
int length,
String suffix)
Shorten string to given length.
|
static String[] |
split(String src,
String delimiter)
Splits a string in several parts (tokens) that are separated by
delimiter.
|
static String[] |
splitc(String src,
char delimiter)
Splits a string in several parts (tokens) that are separated by single
delimiter characters.
|
static String[] |
splitc(String src,
String d)
Splits a string in several parts (tokens) that are separated by delimiter
characters.
|
static boolean |
startsWithChar(String s,
char c)
Returns if string starts with given character.
|
static boolean |
startsWithIgnoreCase(String src,
String subS)
Tests if this string starts with the specified prefix with ignored case.
|
static boolean |
startsWithIgnoreCase(String src,
String subS,
int startIndex)
Tests if this string starts with the specified prefix with ignored case
and with the specified prefix beginning a specified index.
|
static int |
startsWithOne(String src,
String[] dest)
Checks if string starts with at least one string from the provided array.
|
static int |
startsWithOneIgnoreCase(String src,
String[] dest)
Checks if string starts with at least one string from the provided array.
|
static String |
stripLeadingChar(String string,
char c)
Strips leading char if string starts with one.
|
static String |
stripTrailingChar(String string,
char c)
Strips trailing char if string ends with one.
|
static String |
suffix(String string,
String suffix)
Appends suffix if doesn't exist.
|
static String |
surround(String string,
String fix) |
static String |
surround(String string,
String prefix,
String suffix)
Surrounds the string with provided prefix and suffix if such missing from
string.
|
static String |
title(String string)
Makes a title-cased string from given input.
|
static String |
toCamelCase(String input,
boolean firstCharUppercase,
char separator)
Converts separated string value to CamelCase.
|
static String |
toSafeString(Object obj)
Converts safely an object to a string.
|
static String |
toString(Object obj)
Converts safely an object to a string.
|
static String[] |
toStringArray(Object obj)
Converts an object to a String Array.
|
static void |
trimAll(String[] strings)
Trims array of strings.
|
static String |
trimDown(String string)
Trims string and sets to
null if trimmed string is empty. |
static void |
trimDownAll(String[] strings)
Trims array of strings where empty strings are set to
null. |
static String |
trimLeft(String src)
Trim whitespaces from the left.
|
static String |
trimRight(String src)
Trim whitespaces from the right.
|
static String |
truncate(String string,
int length)
Sets the maximum length of the string.
|
static String |
uncapitalize(String str)
Uncapitalizes a
String, changing the first letter to lower
case. |
static String |
unescapeJava(String str)
Unescapes a string using java rules.
|
public static boolean isEmpty(String string)
null or zero-length).string - public static boolean compare(String a, String b)
a - b - public static boolean compareIgnoreCase(String a, String b)
a - b - public static String copy(String src, int len)
src - len - public static String replace(String s, String sub, String with)
s - string to be inspectedsub - string pattern to be replacedwith - string that should go where the pattern waspublic static String replaceChar(String s, char sub, char with)
s - input stringsub - character to replacewith - character to replace withpublic static String replaceChars(String s, char[] sub, char[] with)
s - input stringsub - characters to replacewith - characters to replace withpublic static String replaceFirst(String s, String sub, String with)
s - source stringsub - substring to replacewith - substring to replace withpublic static String replaceFirst(String s, char sub, char with)
s - stringsub - char to replacewith - char to replace withpublic static String replaceLast(String s, String sub, String with)
s - source stringsub - substring to replacewith - substring to replace withpublic static String replaceLast(String s, char sub, char with)
s - stringsub - char to replacewith - char to replace withpublic static String remove(String s, String sub)
s - source stringsub - substring to removepublic static String removeChars(String src, String chars)
src - source stringchars - string containing characters to removepublic static String removeChars(String src, char... chars)
src - stringchars - characters to removepublic static String remove(String string, char ch)
string - source stringch - character to removepublic static boolean equals(String s1, String s2)
null,
false is returned. if both string are null,
true is returned.s1 - first string to compares2 - second stringtrue if strings are equal, otherwise
falsepublic static boolean isAllEmpty(String... strings)
isEmpty(String)public static boolean isBlank(String string)
null or
containsOnlyWhitespaces(String)).public static boolean isNotBlank(String string)
public static boolean isAllBlank(String... strings)
public static boolean containsOnlyWhitespaces(String string)
true if string contains only white spaces.public static boolean containsOnlyDigits(String string)
true if string contains only digits.public static boolean isNotEmpty(String string)
public static String toString(Object obj)
null it
will be not converted.public static String toSafeString(Object obj)
null an
empty string is returned.public static String[] toStringArray(Object obj)
public static String capitalize(String str)
str - string to capitalize, may be nulluncapitalize(String)public static String uncapitalize(String str)
String, changing the first letter to lower
case. No other letters are changed.str - the String to uncapitalize, may be nullnull if nullcapitalize(String)public static String truncate(String string, int length)
public static String[] split(String src, String delimiter)
Method is much, much faster then regexp String.split(), and
a bit faster then StringTokenizer.
src - string to splitdelimiter - split delimiterpublic static String[] splitc(String src, String d)
src - source to examined - string with delimiter characterspublic static String[] splitc(String src, char delimiter)
src - source to examinedelimiter - delimiter characterpublic static String compressChars(String s, char c)
public static int indexOf(String src, String sub, int startIndex, int endIndex)
String.indexOf(String, int) is much faster (since
it uses char[] value directly) and should be used when no range is
needed.src - source string for examinationsub - substring to findstartIndex - starting indexendIndex - ending indexpublic static int indexOf(String src, char c, int startIndex, int endIndex)
public static int indexOfIgnoreCase(String src, char c, int startIndex, int endIndex)
public static int indexOfIgnoreCase(String src, String subS)
src - source string for examinationsubS - substring to findindexOfIgnoreCase(String, String, int)public static int indexOfIgnoreCase(String src, String subS, int startIndex)
src - source string for examinationsubS - substring to findstartIndex - starting index from where search beginspublic static int indexOfIgnoreCase(String src, String sub, int startIndex, int endIndex)
src - source string for examinationsub - substring to findstartIndex - starting index from where search beginsendIndex - endint indexindexOfIgnoreCase(String, String, int)public static int lastIndexOfIgnoreCase(String s, String subS)
s - source stringsubS - substring to findindexOfIgnoreCase(String, String, int),
lastIndexOfIgnoreCase(String, String, int)public static int lastIndexOfIgnoreCase(String src, String subS, int startIndex)
src - source string for examinationsubS - substring to findstartIndex - starting index from where search beginsindexOfIgnoreCase(String, String, int)public static int lastIndexOfIgnoreCase(String src, String sub, int startIndex, int endIndex)
src - source to examinesub - substring to findstartIndex - starting indexendIndex - end indexpublic static int lastIndexOf(String src, String sub, int startIndex, int endIndex)
indexOf(String, String, int, int) for
details about the speed.src - source to examinesub - substring to findstartIndex - starting indexendIndex - end indexpublic static int lastIndexOf(String src, char c, int startIndex, int endIndex)
public static int lastIndexOfIgnoreCase(String src, char c, int startIndex, int endIndex)
public static int lastIndexOfWhitespace(String src)
public static int lastIndexOfWhitespace(String src, int startIndex)
public static int lastIndexOfWhitespace(String src, int startIndex, int endIndex)
public static int lastIndexOfNonWhitespace(String src)
public static int lastIndexOfNonWhitespace(String src, int startIndex)
public static int lastIndexOfNonWhitespace(String src, int startIndex, int endIndex)
public static boolean startsWithIgnoreCase(String src, String subS)
src - source string to testsubS - starting substringtrue if the character sequence represented by the
argument is a prefix of the character sequence represented by
this string; false otherwise.public static boolean startsWithIgnoreCase(String src, String subS, int startIndex)
src - source string to testsubS - starting substringstartIndex - index from where to testtrue if the character sequence represented by the
argument is a prefix of the character sequence represented by
this string; false otherwise.public static boolean endsWithIgnoreCase(String src, String subS)
src - String to testsubS - suffixtrue if the character sequence represented by the
argument is a suffix of the character sequence represented by
this object; false otherwise.public static boolean startsWithChar(String s, char c)
public static boolean endsWithChar(String s, char c)
public static int count(String source, String sub)
source - source stringsub - substring to countpublic static int count(String source, char c)
public static int count(String source, char c, int start)
public static int countIgnoreCase(String source, String sub)
source - source stringsub - substring to countpublic static int[] indexOf(String s, String[] arr)
null
if noting found.s - source stringarr - string arraypublic static int[] indexOf(String s, String[] arr, int start)
null
if noting found.s - source stringarr - string arraystart - starting positionpublic static int[] indexOfIgnoreCase(String s, String[] arr)
null
if noting found.s - source stringarr - string arraypublic static int[] indexOfIgnoreCase(String s, String[] arr, int start)
null
if noting found.s - source stringarr - string arraystart - starting positionpublic static int[] lastIndexOf(String s, String[] arr)
null
if noting found.s - source stringarr - string arraypublic static int[] lastIndexOf(String s, String[] arr, int fromIndex)
null
if noting found.s - source stringarr - string arrayfromIndex - starting positionpublic static int[] lastIndexOfIgnoreCase(String s, String[] arr)
null
if noting found.s - source stringarr - string arraypublic static int[] lastIndexOfIgnoreCase(String s, String[] arr, int fromIndex)
null
if noting found.s - source stringarr - string arrayfromIndex - starting positionpublic static boolean equals(String[] as, String[] as1)
as - first string arrayas1 - second string arraytrue if all array elements matchespublic static boolean equalsIgnoreCase(String[] as, String[] as1)
as - first string arrayas1 - second string arraypublic static String replace(String s, String[] sub, String[] with)
s - source stringsub - substrings arraywith - replace with arraypublic static String replaceIgnoreCase(String s, String[] sub, String[] with)
s - source stringsub - substrings arraywith - replace with arraypublic static int equalsOne(String src, String[] dest)
-1
is returned.public static int equalsOneIgnoreCase(String src, String[] dest)
-1 is returned.public static int startsWithOne(String src, String[] dest)
-1 is returned.public static int startsWithOneIgnoreCase(String src, String[] dest)
-1 is returned.public static int endsWithOne(String src, String[] dest)
-1 is returned.public static int endsWithOneIgnoreCase(String src, String[] dest)
-1 is returned.public static int indexOfChars(String string, String chars, int startindex)
-1 if nothing found.public static int indexOfChars(String string, char[] chars)
public static int indexOfChars(String string, char[] chars, int startindex)
-1 if nothing found.public static int indexOfWhitespace(String string)
public static int indexOfWhitespace(String string, int startindex)
public static int indexOfWhitespace(String string, int startindex, int endindex)
public static int indexOfNonWhitespace(String string)
public static int indexOfNonWhitespace(String string, int startindex)
public static int indexOfNonWhitespace(String string, int startindex, int endindex)
public static String stripLeadingChar(String string, char c)
public static String stripTrailingChar(String string, char c)
public static void trimAll(String[] strings)
null array elements are ignored.public static void trimDownAll(String[] strings)
null.
null elements of the array are ignored.trimDown(String)public static String trimDown(String string)
null if trimmed string is empty.public static void cropAll(String[] strings)
public static int[] indexOfRegion(String string, String leftBoundary, String rightBoundary)
public static int[] indexOfRegion(String string, String leftBoundary, String rightBoundary, int offset)
public static int[] indexOfRegion(String string, String leftBoundary, String rightBoundary, char escape)
public static int[] indexOfRegion(String string, String leftBoundary, String rightBoundary, char escape, int offset)
Escape character may be used to prefix boundaries so they can be ignored.
Double escaped region will be found, and first index of the result will
be decreased to include one escape character. If region is not founded,
null is returned.
public static String join(Iterable<?> elements, String separator)
null.public static String convertCharset(String source, String srcCharsetName, String newCharsetName) throws UnsupportedEncodingException
public static boolean isCharAtEqual(String string, int index, char charToCompare)
public static String surround(String string, String prefix, String suffix)
public static String cutToIndexOf(String string, String substring)
public static String cutToIndexOf(String string, char c)
public static String cutFromIndexOf(String string, String substring)
public static String cutFromIndexOf(String string, char c)
public static String cutSurrounding(String string, String prefix, String suffix)
public static boolean isCharAtEscaped(String src, int ndx, char escapeChar)
true if character at provided index position is
escaped by escape character.public static int indexOfUnescapedChar(String src, char sub, char escapeChar)
public static int indexOfUnescapedChar(String src, char sub, char escapeChar, int startIndex)
public static String insert(String src, String insert, int offset)
public static String repeat(String source, int count)
public static String repeat(char c, int count)
public static String maxCommonPrefix(String one, String two)
public static String fromCamelCase(String input, char separator)
theFOO to the_foo
).
Foo to foo and not
_foo)
user_Name to user_name and not
user__name.
public static String toCamelCase(String input, boolean firstCharUppercase, char separator)
public static String findCommonPrefix(String... strings)
Copyright © 2016. All rights reserved.