类 StringUtils
- 作者:
- lingyi
-
字段概要
字段 -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明static StringcamelToSplitName(String camelName, String split) static booleancontains(CharSequence str1, CharSequence str2) 第一个字符串是否包含第二个字符串static booleanendsWithIgnoreCase(CharSequence str, CharSequence suffix) static booleanequals(CharSequence str1, CharSequence str2, boolean ignoreCase) 比较两个字符串是否相等。static String格式化文本, {} 表示占位符
此方法只是简单将占位符 {} 按照顺序替换为参数
如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可
例:
通常使用:format("this is {} for {}", "a", "b") =》 this is a for b
转义{}: format("this is \\{} for {}", "a", "b") =》 this is \{} for a
转义\: format("this is \\\\{} for {}", "a", "b") =》 this is \a for bstatic booleanisBlank(CharSequence cs) static booleanisEmpty(CharSequence str) is empty string.static booleanstatic booleanisNotEmpty(CharSequence str) is not empty string.static StringlenientFormat(String template, Object... args) static Stringstatic Stringstatic Stringstatic StringremoveAll(CharSequence str, char... chars) 去除字符串中指定的多个字符,如有多个则全部去除static booleanstartWith(CharSequence str, CharSequence prefix, boolean isIgnoreCase) 是否以指定字符串开头
如果给定的字符串和开头字符串都为null则返回true,否则任意一个值为null返回falsestatic booleanstartWithIgnoreCase(CharSequence str, CharSequence prefix) 是否以指定字符串开头,忽略大小写static Stringstr(CharSequence cs) CharSequence转为字符串,null安全static StringStrips whitespace from the start and end of a String.static StringStrips any of a set of characters from the start and end of a String.static String[]Strips whitespace from the start and end of every String in an array.static String[]Strips any of a set of characters from the start and end of every String in an array.static StringStrips any of a set of characters from the end of a String.static StringstripStart(String str, String stripChars) Strips any of a set of characters from the start of a String.static StringstripToEmpty(String str) Strips whitespace from the start and end of a String returning an empty String ifnullinput.static StringstripToNull(String str) Strips whitespace from the start and end of a String returningnullif the String is empty ("") after the strip.static Stringsub(CharSequence str, int fromIndex, int toIndex) 改进JDK subString
index从0开始计算,最后一个字符为-1
如果from和to位置一样,返回 ""
如果from或to为负数,则按照length从后向前数位置,如果绝对值大于字符串长度,则from归到0,to归到length
如果经过修正的index中from大于to,则互换from和to example:
abcdefgh 2 3 =》 c
abcdefgh 2 -3 =》 cdestatic StringsubBefore(CharSequence string, char separator, boolean isLastSeparator) 截取分隔字符串之前的字符串,不包括分隔字符串
如果给定的字符串为空串(null或"")或者分隔字符串为null,返回原字符串
如果分隔字符串未找到,返回原字符串,举例如下:static StringsubPre(CharSequence string, int toIndex) 切割指定位置之前部分的字符串static Stringstatic Stringtrim(CharSequence str) 除去字符串头尾部的空白,如果字符串是null,依然返回null。static Stringtrim(CharSequence str, int mode) 除去字符串头尾部的空白符,如果字符串是null,依然返回null。static StringtrimToEmpty(String str) Removes control characters (char <= 32) from both ends of this String returning an empty String ("") if the String is empty ("") after the trim or if it isnull.static StringtrimToNull(String str) Removes control characters (char <= 32) from both ends of this String returningnullif the String is empty ("") after the trim or if it isnull.
-
字段详细资料
-
INDEX_NOT_FOUND
public static final int INDEX_NOT_FOUND- 另请参阅:
-
EMPTY_JSON
- 另请参阅:
-
BACKSLASH
public static final char BACKSLASH- 另请参阅:
-
DELIMIT_START
public static final char DELIMIT_START- 另请参阅:
-
NULL
- 另请参阅:
-
EMPTY
- 另请参阅:
-
DOT
- 另请参阅:
-
COMMA
- 另请参阅:
-
SEMICOLON
- 另请参阅:
-
UTF_8
UTF-8- 另请参阅:
-
-
构造器详细资料
-
StringUtils
public StringUtils()
-
-
方法详细资料
-
isNotEmpty
is not empty string.- 参数:
str- source string.- 返回:
- is not empty.
-
isEmpty
is empty string.- 参数:
str- source string.- 返回:
- is empty.
-
isNotBlank
-
isBlank
-
format
格式化文本, {} 表示占位符
此方法只是简单将占位符 {} 按照顺序替换为参数
如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可
例:
通常使用:format("this is {} for {}", "a", "b") =》 this is a for b
转义{}: format("this is \\{} for {}", "a", "b") =》 this is \{} for a
转义\: format("this is \\\\{} for {}", "a", "b") =》 this is \a for b- 参数:
template- 文本模板,被替换的部分用 {} 表示params- 参数值- 返回:
- 格式化后的文本
-
subPre
切割指定位置之前部分的字符串- 参数:
string- 字符串toIndex- 切割到的位置(不包括)- 返回:
- 切割后的剩余的前半部分字符串
-
sub
改进JDK subString
index从0开始计算,最后一个字符为-1
如果from和to位置一样,返回 ""
如果from或to为负数,则按照length从后向前数位置,如果绝对值大于字符串长度,则from归到0,to归到length
如果经过修正的index中from大于to,则互换from和to example:
abcdefgh 2 3 =》 c
abcdefgh 2 -3 =》 cde- 参数:
str- StringfromIndex- 开始的index(包括)toIndex- 结束的index(不包括)- 返回:
- 字串
-
str
CharSequence转为字符串,null安全- 参数:
cs-CharSequence- 返回:
- 字符串
-
equals
比较两个字符串是否相等。- 参数:
str1- 要比较的字符串1str2- 要比较的字符串2ignoreCase- 是否忽略大小写- 返回:
- 如果两个字符串相同,或者都是
null,则返回true
-
contains
第一个字符串是否包含第二个字符串- 参数:
str1- 字符串1str2- 字符串2
-
toString
- 参数:
e-- 返回:
- string
-
camelToSplitName
-
trim
除去字符串头尾部的空白,如果字符串是null,依然返回null。注意,和
String.trim不同,此方法使用NumberUtil.isBlankChar来判定空白, 因而可以除去英文字符集之外的其它空白,如中文空格。trim(null) = null trim("") = "" trim(" ") = "" trim("abc") = "abc" trim(" abc ") = "abc"- 参数:
str- 要处理的字符串- 返回:
- 除去头尾空白的字符串,如果原字串为
null,则返回null
-
trim
除去字符串头尾部的空白符,如果字符串是null,依然返回null。- 参数:
str- 要处理的字符串mode--1表示trimStart,0表示trim全部,1表示trimEnd- 返回:
- 除去指定字符后的的字符串,如果原字串为
null,则返回null
-
trimToNull
Removes control characters (char <= 32) from both ends of this String returning
nullif the String is empty ("") after the trim or if it isnull.The String is trimmed using
String.trim(). Trim removes start and end characters <= 32. To strip whitespace usestripToNull(String).StringUtils.trimToNull(null) = null StringUtils.trimToNull("") = null StringUtils.trimToNull(" ") = null StringUtils.trimToNull("abc") = "abc" StringUtils.trimToNull(" abc ") = "abc"- 参数:
str- the String to be trimmed, may be null- 返回:
- the trimmed String,
nullif only chars <= 32, empty or null String input - 从以下版本开始:
- 2.0
-
trimToEmpty
Removes control characters (char <= 32) from both ends of this String returning an empty String ("") if the String is empty ("") after the trim or if it is
null.The String is trimmed using
String.trim(). Trim removes start and end characters <= 32. To strip whitespace usestripToEmpty(String).StringUtils.trimToEmpty(null) = "" StringUtils.trimToEmpty("") = "" StringUtils.trimToEmpty(" ") = "" StringUtils.trimToEmpty("abc") = "abc" StringUtils.trimToEmpty(" abc ") = "abc"- 参数:
str- the String to be trimmed, may be null- 返回:
- the trimmed String, or an empty String if
nullinput - 从以下版本开始:
- 2.0
-
strip
Strips whitespace from the start and end of a String.
Whitespace is defined by
Character.isWhitespace(char).A
nullinput String returnsnull.StringUtils.strip(null) = null StringUtils.strip("") = "" StringUtils.strip(" ") = "" StringUtils.strip("abc") = "abc" StringUtils.strip(" abc") = "abc" StringUtils.strip("abc ") = "abc" StringUtils.strip(" abc ") = "abc" StringUtils.strip(" ab c ") = "ab c"- 参数:
str- the String to remove whitespace from, may be null- 返回:
- the stripped String,
nullif null String input
-
stripToNull
Strips whitespace from the start and end of a String returning
nullif the String is empty ("") after the strip.This is similar to
trimToNull(String)but removes whitespace. Whitespace is defined byCharacter.isWhitespace(char).StringUtils.stripToNull(null) = null StringUtils.stripToNull("") = null StringUtils.stripToNull(" ") = null StringUtils.stripToNull("abc") = "abc" StringUtils.stripToNull(" abc") = "abc" StringUtils.stripToNull("abc ") = "abc" StringUtils.stripToNull(" abc ") = "abc" StringUtils.stripToNull(" ab c ") = "ab c"- 参数:
str- the String to be stripped, may be null- 返回:
- the stripped String,
nullif whitespace, empty or null String input - 从以下版本开始:
- 2.0
-
stripToEmpty
Strips whitespace from the start and end of a String returning an empty String if
nullinput.This is similar to
trimToEmpty(String)but removes whitespace. Whitespace is defined byCharacter.isWhitespace(char).StringUtils.stripToEmpty(null) = "" StringUtils.stripToEmpty("") = "" StringUtils.stripToEmpty(" ") = "" StringUtils.stripToEmpty("abc") = "abc" StringUtils.stripToEmpty(" abc") = "abc" StringUtils.stripToEmpty("abc ") = "abc" StringUtils.stripToEmpty(" abc ") = "abc" StringUtils.stripToEmpty(" ab c ") = "ab c"- 参数:
str- the String to be stripped, may be null- 返回:
- the trimmed String, or an empty String if
nullinput - 从以下版本开始:
- 2.0
-
strip
Strips any of a set of characters from the start and end of a String. This is similar to
String.trim()but allows the characters to be stripped to be controlled.A
nullinput String returnsnull. An empty string ("") input returns the empty string.If the stripChars String is
null, whitespace is stripped as defined byCharacter.isWhitespace(char). Alternatively usestrip(String).StringUtils.strip(null, *) = null StringUtils.strip("", *) = "" StringUtils.strip("abc", null) = "abc" StringUtils.strip(" abc", null) = "abc" StringUtils.strip("abc ", null) = "abc" StringUtils.strip(" abc ", null) = "abc" StringUtils.strip(" abcyx", "xyz") = " abc"- 参数:
str- the String to remove characters from, may be nullstripChars- the characters to remove, null treated as whitespace- 返回:
- the stripped String,
nullif null String input
-
stripStart
Strips any of a set of characters from the start of a String.
A
nullinput String returnsnull. An empty string ("") input returns the empty string.If the stripChars String is
null, whitespace is stripped as defined byCharacter.isWhitespace(char).StringUtils.stripStart(null, *) = null StringUtils.stripStart("", *) = "" StringUtils.stripStart("abc", "") = "abc" StringUtils.stripStart("abc", null) = "abc" StringUtils.stripStart(" abc", null) = "abc" StringUtils.stripStart("abc ", null) = "abc " StringUtils.stripStart(" abc ", null) = "abc " StringUtils.stripStart("yxabc ", "xyz") = "abc "- 参数:
str- the String to remove characters from, may be nullstripChars- the characters to remove, null treated as whitespace- 返回:
- the stripped String,
nullif null String input
-
stripEnd
Strips any of a set of characters from the end of a String.
A
nullinput String returnsnull. An empty string ("") input returns the empty string.If the stripChars String is
null, whitespace is stripped as defined byCharacter.isWhitespace(char).StringUtils.stripEnd(null, *) = null StringUtils.stripEnd("", *) = "" StringUtils.stripEnd("abc", "") = "abc" StringUtils.stripEnd("abc", null) = "abc" StringUtils.stripEnd(" abc", null) = " abc" StringUtils.stripEnd("abc ", null) = "abc" StringUtils.stripEnd(" abc ", null) = " abc" StringUtils.stripEnd(" abcyx", "xyz") = " abc" StringUtils.stripEnd("120.00", ".0") = "12"- 参数:
str- the String to remove characters from, may be nullstripChars- the set of characters to remove, null treated as whitespace- 返回:
- the stripped String,
nullif null String input
-
stripAll
Strips whitespace from the start and end of every String in an array. Whitespace is defined by
Character.isWhitespace(char).A new array is returned each time, except for length zero. A
nullarray will returnnull. An empty array will return itself. Anullarray entry will be ignored.StringUtils.stripAll(null) = null StringUtils.stripAll([]) = [] StringUtils.stripAll(["abc", " abc"]) = ["abc", "abc"] StringUtils.stripAll(["abc ", null]) = ["abc", null]
- 参数:
strs- the array to remove whitespace from, may be null- 返回:
- the stripped Strings,
nullif null array input
-
stripAll
Strips any of a set of characters from the start and end of every String in an array.
Whitespace is defined byCharacter.isWhitespace(char).A new array is returned each time, except for length zero. A
nullarray will returnnull. An empty array will return itself. Anullarray entry will be ignored. AnullstripChars will strip whitespace as defined byCharacter.isWhitespace(char).StringUtils.stripAll(null, *) = null StringUtils.stripAll([], *) = [] StringUtils.stripAll(["abc", " abc"], null) = ["abc", "abc"] StringUtils.stripAll(["abc ", null], null) = ["abc", null] StringUtils.stripAll(["abc ", null], "yz") = ["abc ", null] StringUtils.stripAll(["yabcz", null], "yz") = ["abc", null]
- 参数:
strs- the array to remove characters from, may be nullstripChars- the characters to remove, null treated as whitespace- 返回:
- the stripped Strings,
nullif null array input
-
subBefore
截取分隔字符串之前的字符串,不包括分隔字符串
如果给定的字符串为空串(null或"")或者分隔字符串为null,返回原字符串
如果分隔字符串未找到,返回原字符串,举例如下:StrUtil.subBefore(null, *) = null StrUtil.subBefore("", *) = "" StrUtil.subBefore("abc", 'a') = "" StrUtil.subBefore("abcba", 'b') = "a" StrUtil.subBefore("abc", 'c') = "ab" StrUtil.subBefore("abc", 'd') = "abc"- 参数:
string- 被查找的字符串separator- 分隔字符串(不包括)isLastSeparator- 是否查找最后一个分隔字符串(多次出现分隔字符串时选取最后一个),true为选取最后一个- 返回:
- 切割后的字符串
-
endsWithIgnoreCase
-
startWithIgnoreCase
是否以指定字符串开头,忽略大小写- 参数:
str- 被监测字符串prefix- 开头字符串- 返回:
- 是否以指定字符串开头
-
startWith
是否以指定字符串开头
如果给定的字符串和开头字符串都为null则返回true,否则任意一个值为null返回false- 参数:
str- 被监测字符串prefix- 开头字符串isIgnoreCase- 是否忽略大小写- 返回:
- 是否以指定字符串开头
-
removeAll
去除字符串中指定的多个字符,如有多个则全部去除- 参数:
str- 字符串chars- 字符列表- 返回:
- 去除后的字符
-
padStart
-
padEnd
-
padMiddle
-
lenientFormat
-