public abstract class StringUtil extends Object
| 限定符和类型 | 字段和说明 |
|---|---|
protected static char[] |
digits
用于在2-16进制之间进行转换的映射字符数组
|
| 构造器和说明 |
|---|
StringUtil() |
| 限定符和类型 | 方法和说明 |
|---|---|
static boolean |
containsWord(String container,
String searchedWord,
String seperatorChars)
检测指定字符串中是否存在指定的单词
该方法采用快速模式,对于类似 containsWord("abc123,123", "123", ",") 等特殊情况无法保证100%可靠;如果想要保证可靠性,建议使用 containsWord(String, String, String, boolean) |
static boolean |
containsWord(String container,
String searchedWord,
String seperatorChars,
boolean fastMode)
检测指定字符串中是否存在指定的单词
|
static boolean |
endsWith(String str,
char lastChar)
判断指定字符串是否以指定的单个字符结尾
|
static int[] |
ensureRangeSafe(int beginIndex,
int endIndex,
int length)
确保起始和结束索引的范围边界安全,并返回范围安全的 [开始索引, 结束索引] 数组
如果索引参数为负,自动加上 length,如果处理后的 beginIndex > endIndex,则自动对调位置方法起始索引或结束索引超出合理边界,方法内会尽可能地根据 length 进行调整:1、如果 beginIndex < -length ,则 beginIndex = 02、如果 endIndex > length ,则 endIndex = length |
static String |
escapeSQLLike(String likeStr)
将指定的用于LIKE语句的字符串转义,以防止SQL语句注入
该方法默认使用'\'进行转义操作 |
static String |
escapeSQLLike(String likeStr,
boolean appendLikeWildcard)
将指定的用于LIKE语句的字符串转义,以防止SQL语句注入
该方法默认使用'\'进行转义操作 |
static String |
escapeSQLLike(String likeStr,
char escapeChar,
boolean appendLikeWildcard)
使用指定的转义字符对用于LIKE语句的字符串进行转义,以防止SQL语句注入
|
static String |
fastUnicode(String str)
获取指定字符串的Unicode编码,例如:"中国"将返回"中国"
此方法返回的编码中,字母均采用大写形式,此外,由于编码的长度是已知的,本方法采用char数组作为字符容器,省略了StringBuilder追加时的长度判断以及封装损耗,性能更加优异 |
static StringBuilder |
getBuilder(int extra,
CharSequence s1)
根据将要追加的多个字符序列获得适当初始容量的
StringBuilder |
static StringBuilder |
getBuilder(int extra,
CharSequence s1,
CharSequence s2)
根据将要追加的多个字符序列获得适当初始容量的
StringBuilder |
static StringBuilder |
getBuilder(int extra,
CharSequence s1,
CharSequence s2,
CharSequence s3)
根据将要追加的多个字符序列获得适当初始容量的
StringBuilder |
static StringBuilder |
getBuilder(int extra,
CharSequence s1,
CharSequence s2,
CharSequence s3,
CharSequence s4)
根据将要追加的多个字符序列获得适当初始容量的
StringBuilder |
static StringBuilder |
getBuilder(int size,
int shift)
根据每个元素的平均大小和元素的格式创建一个适用于其容量的StringBuilder
内部采用 length << shift 来获取设置初始容量并且生成的StringBuilder的最小容量为16(StringBuilder的默认容量) |
static boolean |
hasBlank(Object... values)
判断指定数组中是否为空的值(null、空字符串、空格字符串),如果是,将返回true
本方法接受多个参数,如果其中有任意一个为空,就返回true 本方法会去除两边的空格后再判断 如果指定的key不存在也返回true |
static boolean |
hasEmpty(Object... values)
判断指定数组中是否存在为空的值(null、空字符串),如果是将返回true
本方法接受多个参数,如果其中有任意一个为空,就返回true 如果指定的key不存在也返回true |
static boolean |
isAnyNotEmpty(CharSequence... css)
判断指定的字符序列数组是否存在不为空的元素
如果数组中存在不为null、空字符串的元素,则返回true,否则返回false |
static boolean |
isBlank(CharSequence str)
判断指定的字符串是否为空
如果字符串为null、空字符串、空格字符串,则返回true 注意:本方法会先去除字符串两边的空格,再判断 |
static boolean |
isBlank(Object obj)
判断指定的对象是否为空
如果对象(或其toSring()返回值)为null、空字符串、空格字符串,则返回true 注意:本方法会先去除字符串两边的空格,再判断 |
static boolean |
isEmpty(CharSequence str)
判断指定的字符串是否为空
如果字符串为null、空字符串,则返回true,否则返回false 注意:本方法不会去除字符串两边的空格,如果需要对字符串进行去除两边空格后的判断,请使用 StringUtil#isBlank(String str)方法 |
static boolean |
isEmpty(Object obj)
判断指定的对象是否为空
如果对象(或其toSring()返回值)为null、空字符串,则返回true 注意:本方法不会去除字符串两边的空格,如果需要对字符串进行去除两边空格后的判断,请使用isBlank(Object obj)方法 |
static String |
leftPad(String str,
char ch,
int maxLength)
如果字符串不足指定位数,则在前面补充指定的字符,直到指定位数
如果字符串=null,则返回空字符串"" 如果字符串位数大于指定位数,则返回原字符串 |
static int |
length(CharSequence cs)
获取指定字符序列的字符长度
|
static String |
limitChars(String str,
int maxLength)
如果指定字符串超过限制长度
maxLength,则返回限制长度前面的部分字符串如果指定字符串==null,则返回空字符串 如果字符串超出指定长度,则返回maxLength前面的部分,并在末尾加上后缀“...” |
static String |
limitChars(String str,
int maxLength,
String suffix)
如果指定字符串超过限制长度
maxLength,则返回限制长度前面的部分字符串如果指定字符串==null,则返回空字符串 如果字符串超出指定长度,则返回maxLength前面的部分,并在末尾加上后缀 suffix |
static boolean |
notBlank(Object obj)
判断指定的对象是否为空
如果对象(或其toSring()返回值)不为null、空字符串、空格字符串,则返回true,否则返回false 注意:本方法会先去除字符串两边的空格,再判断 |
static boolean |
notBlank(String str)
判断指定的字符串是否不为空
如果字符串不为null、空字符串、空格字符串,则返回true,否则返回false 注意:本方法会先去除字符串两边的空格,再判断 |
static boolean |
notEmpty(CharSequence str)
判断指定的字符串是否不为空
如果指定字符串不为null、空字符串,则返回true,否则返回false 注意:本方法不会去除字符串两边的空格,如果需要对字符串进行去除两边空格后的判断,请使用 notBlank(String str)方法 |
static boolean |
notEmpty(Object obj)
判断指定的对象是否不为空
如果对象(或其toSring()返回值)不为null、空字符串,则返回true,否则返回false 注意:本方法不会去除字符串两边的空格,如果需要对字符串进行去除两边空格后的判断,请使用 isBlank(Object obj)方法 |
static String |
replaceChars(String str,
char ch,
int beginIndex)
将指定字符串的
beginIndex 到末尾之间的字符全部替换为字符 ch |
static String |
replaceChars(String str,
char ch,
int beginIndex,
int endIndex)
将指定字符串的
beginIndex 到 endIndex (不包括 endIndex ) 之间的字符全部替换为字符 ch |
static String |
replaceSubstring(String str,
String replacement,
int beginIndex)
将指定字符串的
beginIndex 及其后的子字符串全部替换为指定的字符串 replacement |
static String |
replaceSubstring(String str,
String replacement,
int beginIndex,
int endIndex)
将指定字符串的
beginIndex 到 endIndex (不包括 endIndex ) 之间的子字符串全部替换为指定的字符串 replacement |
static String |
reverse(CharSequence str)
颠倒(反转)字符串的字符顺序,并返回颠倒后的字符串
如果字符串为null,则返回空字符串"" |
static String |
rightPad(String str,
char ch,
int maxLength)
如果字符串不足指定位数,则在后面补充指定的字符,直到指定位数
如果字符串=null,则返回空字符串"" 如果字符串位数大于指定位数,则返回原字符串 |
static boolean |
startsWith(String str,
char firstChar)
判断指定字符串是否以指定的单个字符开头
|
static String |
toString(Object obj)
以String的形式返回对象值,如果对象为null,则返回""
|
static String |
transEncoding(String str,
String originalCharset,
String targetCharset)
将字符串从指定字符集编码转换为目标字符集编码
|
static String |
transEncodingForURI(String str,
String targetCharset)
将指定的URI参数字符串转换为目标字符集编码
本方法实际上是将字符串从ISO-8859-1编码转换为指定的目标编码 |
static String |
trim(Object obj)
以去除两边空格的String形式返回对象值,如果对象为null,则返回""
|
static String |
trim(String str)
去除字符串两侧的空白字符
如果为null则返回空字符串"" |
static String |
trim4Html(Object obj)
以String的形式返回对象值,如果对象为null或空格字符串,则返回" "
|
static String |
unicode(String src)
获取指定字符串的Unicode编码,例如:“中国”将返回“中国”
此方法返回的编码中,字母均采用大写形式,此外,本方法采用StringBuilder作为字符容器 |
static String |
zeroFill(String str,
int maxLength)
如果字符串不足指定位数,则前面补0,直到指定位数
如果字符串=null,则返回空字符串"" 如果字符串位数大于指定位数,则返回原字符串 |
public static String unicode(String src)
src - 指定字符串不能为null,否则将引发空指针异常public static String fastUnicode(String str)
src - 指定字符串不能为null,否则将引发空指针异常public static final StringBuilder getBuilder(int size, int shift)
length << shift 来获取设置初始容量size - 元素个数shift - 每个元素参与拼接的平均字符数相对于2的位移量public static final int length(CharSequence cs)
cs为null,则返回 0public static final StringBuilder getBuilder(int extra, CharSequence s1, CharSequence s2, CharSequence s3, CharSequence s4)
StringBuilderextra - 除指定的字符序列外应额外预留的字符容量public static final StringBuilder getBuilder(int extra, CharSequence s1, CharSequence s2, CharSequence s3)
StringBuilderextra - 除指定的字符序列外应额外预留的字符容量public static final StringBuilder getBuilder(int extra, CharSequence s1, CharSequence s2)
StringBuilderextra - 除指定的字符序列外应额外预留的字符容量public static final StringBuilder getBuilder(int extra, CharSequence s1)
StringBuilderextra - 除指定的字符序列外应额外预留的字符容量public static final boolean isEmpty(CharSequence str)
StringUtil#isBlank(String str)方法public static final boolean notEmpty(CharSequence str)
notBlank(String str)方法public static final boolean isAnyNotEmpty(CharSequence... css)
public static final boolean isEmpty(Object obj)
obj - 指定的对象public static final boolean notEmpty(Object obj)
isBlank(Object obj)方法obj - 指定的对象public static final boolean hasEmpty(Object... values)
values - 指定的数组isEmpty(Object)public static final boolean isBlank(CharSequence str)
public static final boolean notBlank(String str)
public static final boolean isBlank(Object obj)
obj - 指定的对象public static final boolean notBlank(Object obj)
obj - 指定的对象public static final boolean hasBlank(Object... values)
values - 指定的数组public static final String toString(Object obj)
obj - 指定的对象public static final String trim(Object obj)
obj - 指定的对象public static final String trim4Html(Object obj)
obj - 指定的对象public static final String limitChars(String str, int maxLength, String suffix)
maxLength,则返回限制长度前面的部分字符串suffixstr - 指定的字符串maxLength - 最大限制长度suffix - 超出长度时添加的指定后缀,如果不需要,可以为nullpublic static final String limitChars(String str, int maxLength)
maxLength,则返回限制长度前面的部分字符串str - 指定的字符串maxLength - 最大限制长度public static final String zeroFill(String str, int maxLength)
str - 字符串maxLength - 指定位数,不能小于1public static final String leftPad(String str, char ch, int maxLength)
str - 字符串ch - 指定的字符maxLength - 指定位数,不能小于1public static final String rightPad(String str, char ch, int maxLength)
str - 字符串ch - 指定的字符maxLength - 指定位数,不能小于1public static final String replaceChars(String str, char ch, int beginIndex, int endIndex)
beginIndex 到 endIndex (不包括 endIndex ) 之间的字符全部替换为字符 chstr - 指定的字符串ch - 指定的字符beginIndex - 指定的字符串起始索引(可以为负数,表示 beginIndex + str.length() )endIndex - 指定的字符串结束索引(可以为负数,表示 endIndex + str.length() )public static final int[] ensureRangeSafe(int beginIndex,
int endIndex,
int length)
length,如果处理后的 beginIndex > endIndex,则自动对调位置length 进行调整:beginIndex < -length ,则 beginIndex = 0endIndex > length ,则 endIndex = lengthbeginIndex - 起始索引endIndex - 结束索引length - 指定数组、集合或字符串的长度,不能小于0public static final String replaceChars(String str, char ch, int beginIndex)
beginIndex 到末尾之间的字符全部替换为字符 chstr - 指定的字符串ch - 指定的字符beginIndex - 指定的字符串起始索引(可以为负数,表示 beginIndex + str.length() )public static final String replaceSubstring(String str, String replacement, int beginIndex, int endIndex)
beginIndex 到 endIndex (不包括 endIndex ) 之间的子字符串全部替换为指定的字符串 replacementstr - 指定的字符串replacement - 指定的字符串beginIndex - 指定的字符串起始索引(可以为负数,表示 beginIndex + str.length() )endIndex - 指定的字符串结束索引(可以为负数,表示 endIndex + str.length() )public static final String replaceSubstring(String str, String replacement, int beginIndex)
beginIndex 及其后的子字符串全部替换为指定的字符串 replacementstr - 指定的字符串replacement - 指定的字符串beginIndex - 指定的字符串起始索引(可以为负数,表示 beginIndex + str.length() )public static final String transEncoding(String str, String originalCharset, String targetCharset)
str - 指定的字符串originalCharset - 原始字符集编码targetCharset - 目标字符集编码public static final String transEncodingForURI(String str, String targetCharset)
str - 指定的URI参数字符串targetCharset - 目标字符集编码public static final String reverse(CharSequence str)
str - 指定的字符串public static final boolean startsWith(String str, char firstChar)
str - 指定的字符串firstChar - 指定的单个字符public static final boolean endsWith(String str, char lastChar)
str - 指定的字符串lastChar - 指定的单个字符public static final String escapeSQLLike(String likeStr, char escapeChar, boolean appendLikeWildcard)
likeStr - 指定的字符串escapeChar - 转义字符appendLikeWildcard - 是否需要在字符串两侧添加通配符'%'public static final String escapeSQLLike(String likeStr, boolean appendLikeWildcard)
likeStr - 指定的字符串appendLikeWildcard - 是否需要在字符串两侧添加通配符'%'public static final String escapeSQLLike(String likeStr)
likeStr - 指定的字符串public static final boolean containsWord(String container, String searchedWord, String seperatorChars, boolean fastMode)
container - 待检测的字符串searchedWord - 指定的单词seperatorChars - 单词两侧必须是指定的字符之一或位于字符串 container 的首/尾位置public static final boolean containsWord(String container, String searchedWord, String seperatorChars)
containsWord("abc123,123", "123", ",") 等特殊情况无法保证100%可靠;如果想要保证可靠性,建议使用 containsWord(String, String, String, boolean)container - 待检测的字符串searchedWord - 指定的单词seperatorChars - 单词两侧必须是指定的字符之一或位于字符串 container 的首/尾位置StringUtil#containsWord(String, String, String, boolean) }Copyright © 2019. All rights reserved.