public class StrUtil extends CharSequenceUtil implements StrPool
EMPTY, INDEX_NOT_FOUND, NULL, SPACE
AT, BACKSLASH, BRACKET_END, BRACKET_START, C_AT, C_BACKSLASH, C_BRACKET_END, C_BRACKET_START, C_COLON, C_COMMA, C_CR, C_DELIM_END, C_DELIM_START, C_DOT, C_LF, C_SLASH, C_SPACE, C_TAB, C_UNDERLINE, COLON, COMMA, CR, CRLF, DASHED, DELIM_END, DELIM_START, DOT, DOUBLE_DOT, EMPTY_JSON, HTML_AMP, HTML_APOS, HTML_GT, HTML_LT, HTML_NBSP, HTML_QUOTE, LF, SLASH, TAB, UNDERLINE
Constructor and Description |
---|
StrUtil() |
Modifier and Type | Method and Description |
---|---|
static StringBuilder |
builder()
创建StringBuilder对象
|
static StringBuilder |
builder(int capacity)
创建StringBuilder对象
|
static String |
fill(String str,
char filledChar,
int len,
boolean isPre)
将已有字符串填充为规定长度,如果已有字符串超过这个长度则返回这个字符串
|
static String |
fillAfter(String str,
char filledChar,
int len)
将已有字符串填充为规定长度,如果已有字符串超过这个长度则返回这个字符串
字符填充于字符串后 |
static String |
fillBefore(String str,
char filledChar,
int len)
将已有字符串填充为规定长度,如果已有字符串超过这个长度则返回这个字符串
字符填充于字符串前 |
static String |
format(CharSequence template,
Map<?,?> map)
格式化文本,使用 {varName} 占位
map = {a: "aValue", b: "bValue"} format("{a} and {b}", map) ---=》 aValue and bValue |
static String |
format(CharSequence template,
Map<?,?> map,
boolean ignoreNull)
格式化文本,使用 {varName} 占位
map = {a: "aValue", b: "bValue"} format("{a} and {b}", map) ---=》 aValue and bValue |
static StringReader |
getReader(CharSequence str)
获得StringReader
|
static StringWriter |
getWriter()
获得StringWriter
|
static boolean |
isBlankIfStr(Object obj)
如果对象是字符串是否为空白,空白的定义如下:
null
空字符串:""
空格、全角空格、制表符、换行符,等不可见字符
例:
StrUtil.isBlankIfStr(null) // true
StrUtil.isBlankIfStr("") // true
StrUtil.isBlankIfStr(" \t\n") // true
StrUtil.isBlankIfStr("abc") // false
注意:该方法与 isEmptyIfStr(Object) 的区别是:
该方法会校验空白字符,且性能相对于 isEmptyIfStr(Object) 略慢。 |
static boolean |
isEmptyIfStr(Object obj)
如果对象是字符串是否为空串,空的定义如下:
null
空字符串:""
例:
StrUtil.isEmptyIfStr(null) // true
StrUtil.isEmptyIfStr("") // true
StrUtil.isEmptyIfStr(" \t\n") // false
StrUtil.isEmptyIfStr("abc") // false
注意:该方法与 isBlankIfStr(Object) 的区别是:该方法不校验空白字符。 |
static String |
reverse(String str)
反转字符串
例如:abcd =》dcba |
static double |
similar(String str1,
String str2)
计算两个字符串的相似度
|
static String |
similar(String str1,
String str2,
int scale)
计算两个字符串的相似度百分比
|
static String |
str(byte[] data,
Charset charset)
解码字节码
|
static String |
str(Byte[] data,
Charset charset)
解码字节码
|
static String |
str(byte[] bytes,
String charset)
将byte数组转为字符串
|
static String |
str(Byte[] bytes,
String charset)
将Byte数组转为字符串
|
static String |
str(ByteBuffer data,
Charset charset)
将编码的byteBuffer数据转换为字符串
|
static String |
str(ByteBuffer data,
String charset)
将编码的byteBuffer数据转换为字符串
|
static String |
str(Object obj,
Charset charset)
将对象转为字符串
1、Byte数组和ByteBuffer会被转换为对应字符串的数组
2、对象数组会调用Arrays.toString方法
|
static String |
str(Object obj,
String charsetName)
Deprecated.
|
static StrBuilder |
strBuilder()
创建StrBuilder对象
|
static StrBuilder |
strBuilder(int capacity)
创建StrBuilder对象
|
static String |
toString(Object obj)
调用对象的toString方法,null会返回“null”
|
static String |
toStringOrEmpty(Object obj)
调用对象的toString方法,null会返回空字符串 ""
|
static String |
toStringOrNull(Object obj)
调用对象的toString方法,null会返回
null |
static void |
trim(String[] strs)
给定字符串数组全部做去首尾空格
|
static String |
truncateByByteLength(String str,
Charset charset,
int maxBytes,
int factor,
boolean appendDots)
截断字符串,使用其按照指定编码为字节后不超过maxBytes长度
此方法用于截取总bytes数不超过指定长度,如果字符出没有超出原样输出,如果超出了,则截取掉超出部分,并可选添加..., 但是添加“...”后总长度也不超过限制长度。 |
static String |
truncateUtf8(String str,
int maxBytes)
截断字符串,使用其按照UTF-8编码为字节后不超过maxBytes长度。
|
static String |
utf8Str(Object obj)
将对象转为字符串
1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法 |
static String |
uuid()
生成随机UUID
|
addPrefixIfNot, addSuffixIfNot, appendIfMissing, appendIfMissing, appendIfMissingIgnoreCase, blankToDefault, brief, builder, byteBuffer, byteLength, bytes, bytes, bytes, center, center, center, cleanBlank, commonPrefix, commonSuffix, compare, compareIgnoreCase, compareVersion, concat, contains, contains, containsAll, containsAny, containsAny, containsAnyIgnoreCase, containsBlank, containsIgnoreCase, containsOnly, count, count, cut, desensitized, emptyIfNull, emptyToDefault, emptyToNull, endWith, endWith, endWith, endWith, endWithAny, endWithAnyIgnoreCase, endWithIgnoreCase, equals, equals, equalsAny, equalsAny, equalsAnyIgnoreCase, equalsCharAt, equalsIgnoreCase, filter, firstNonBlank, firstNonEmpty, firstNonNull, fixLength, format, genGetter, genSetter, getContainsStr, getContainsStrIgnoreCase, getGeneralField, hasBlank, hasEmpty, hasLetter, hide, indexedFormat, indexOf, indexOf, indexOf, indexOf, indexOfIgnoreCase, indexOfIgnoreCase, isAllBlank, isAllCharMatch, isAllEmpty, isAllNotBlank, isAllNotEmpty, isBlank, isBlankOrUndefined, isCharEquals, isEmpty, isEmptyOrUndefined, isLowerCase, isNotBlank, isNotEmpty, isNullOrUndefined, isNumeric, isSubEquals, isSubEquals, isSurround, isSurround, isUpperCase, isWrap, isWrap, isWrap, isWrap, join, join, lastIndexOf, lastIndexOfIgnoreCase, lastIndexOfIgnoreCase, length, lowerFirst, maxLength, move, normalize, nullToDefault, nullToEmpty, ordinalIndexOf, padAfter, padAfter, padPre, padPre, prependIfMissing, prependIfMissing, prependIfMissingIgnoreCase, removeAll, removeAll, removeAllLineBreaks, removeAllPrefix, removeAllSuffix, removeAny, removePreAndLowerFirst, removePreAndLowerFirst, removePrefix, removePrefixIgnoreCase, removeSufAndLowerFirst, removeSuffix, removeSuffixIgnoreCase, repeat, repeat, repeatAndJoin, repeatByLength, replace, replace, replace, replace, replace, replace, replace, replaceByCodePoint, replaceByCodePoint, replaceChars, replaceChars, replaceFirst, replaceFirst, replaceIgnoreCase, replaceLast, replaceLast, split, split, split, split, split, split, split, split, split, splitToArray, splitToArray, splitToArray, splitToInt, splitToInt, splitToLong, splitToLong, splitTrim, splitTrim, splitTrim, splitTrim, startWith, startWith, startWith, startWith, startWithAny, startWithAnyIgnoreCase, startWithIgnoreCase, startWithIgnoreEquals, str, strBuilder, strip, strip, strip, stripAll, stripAll, stripIgnoreCase, stripIgnoreCase, sub, subAfter, subAfter, subBefore, subBefore, subBetween, subBetween, subBetweenAll, subBetweenAll, subByCodePoint, subPre, subPreGbk, subPreGbk, subSuf, subSufByLength, subWithLength, swapCase, toCamelCase, toCamelCase, toSymbolCase, totalLength, toUnderlineCase, trim, trim, trim, trimEnd, trimStart, trimToEmpty, trimToNull, unWrap, unWrap, unWrap, upperFirst, upperFirstAndAddPre, utf8Bytes, wrap, wrap, wrapAll, wrapAllIfMissing, wrapAllWithPair, wrapAllWithPairIfMissing, wrapIfMissing
public static boolean isBlankIfStr(Object obj)
如果对象是字符串是否为空白,空白的定义如下:
null
""
例:
StrUtil.isBlankIfStr(null) // true
StrUtil.isBlankIfStr("") // true
StrUtil.isBlankIfStr(" \t\n") // true
StrUtil.isBlankIfStr("abc") // false
注意:该方法与 isEmptyIfStr(Object)
的区别是:
该方法会校验空白字符,且性能相对于 isEmptyIfStr(Object)
略慢。
obj
- 对象CharSequenceUtil.isBlank(CharSequence)
public static boolean isEmptyIfStr(Object obj)
如果对象是字符串是否为空串,空的定义如下:
null
""
例:
StrUtil.isEmptyIfStr(null) // true
StrUtil.isEmptyIfStr("") // true
StrUtil.isEmptyIfStr(" \t\n") // false
StrUtil.isEmptyIfStr("abc") // false
注意:该方法与 isBlankIfStr(Object)
的区别是:该方法不校验空白字符。
obj
- 对象public static void trim(String[] strs)
strs
- 字符串数组public static String utf8Str(Object obj)
1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法
obj
- 对象@Deprecated public static String str(Object obj, String charsetName)
str(Object, Charset)
1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法
obj
- 对象charsetName
- 字符集public static String str(Object obj, Charset charset)
1、Byte数组和ByteBuffer会被转换为对应字符串的数组 2、对象数组会调用Arrays.toString方法
obj
- 对象charset
- 字符集public static String str(byte[] bytes, String charset)
bytes
- byte数组charset
- 字符集public static String str(byte[] data, Charset charset)
data
- 字符串charset
- 字符集,如果此字段为空,则解码的结果取决于平台public static String str(Byte[] bytes, String charset)
bytes
- byte数组charset
- 字符集public static String str(Byte[] data, Charset charset)
data
- 字符串charset
- 字符集,如果此字段为空,则解码的结果取决于平台public static String str(ByteBuffer data, String charset)
data
- 数据charset
- 字符集,如果为空使用当前系统字符集public static String str(ByteBuffer data, Charset charset)
data
- 数据charset
- 字符集,如果为空使用当前系统字符集public static String toString(Object obj)
obj
- 对象String.valueOf(Object)
public static String toStringOrNull(Object obj)
null
obj
- 对象null
public static String toStringOrEmpty(Object obj)
obj
- 对象String
public static StringBuilder builder()
public static StrBuilder strBuilder()
public static StringBuilder builder(int capacity)
capacity
- 初始大小public static StrBuilder strBuilder(int capacity)
capacity
- 初始大小public static StringReader getReader(CharSequence str)
str
- 字符串public static StringWriter getWriter()
public static String reverse(String str)
str
- 被反转的字符串public static String fillBefore(String str, char filledChar, int len)
str
- 被填充的字符串filledChar
- 填充的字符len
- 填充长度public static String fillAfter(String str, char filledChar, int len)
str
- 被填充的字符串filledChar
- 填充的字符len
- 填充长度public static String fill(String str, char filledChar, int len, boolean isPre)
str
- 被填充的字符串filledChar
- 填充的字符len
- 填充长度isPre
- 是否填充在前public static double similar(String str1, String str2)
str1
- 字符串1str2
- 字符串2public static String similar(String str1, String str2, int scale)
str1
- 字符串1str2
- 字符串2scale
- 相似度public static String uuid()
IdUtil.randomUUID()
public static String format(CharSequence template, Map<?,?> map)
template
- 文本模板,被替换的部分用 {key} 表示map
- 参数值对public static String format(CharSequence template, Map<?,?> map, boolean ignoreNull)
template
- 文本模板,被替换的部分用 {key} 表示map
- 参数值对ignoreNull
- 是否忽略 null
值,忽略则 null
值对应的变量不被替换,否则替换为""public static String truncateUtf8(String str, int maxBytes)
str
- java字符串maxBytes
- 最大字节长度public static String truncateByByteLength(String str, Charset charset, int maxBytes, int factor, boolean appendDots)
str
- 原始字符串charset
- 指定编码maxBytes
- 最大字节数factor
- 速算因子,取该编码下单个字符的最大可能字节数appendDots
- 截断后是否追加省略号(...)Copyright © 2024. All rights reserved.