這個工具的用處類似于Apache Commons Lang中的StringUtil
常用的方法
String str = "abCDEfghi";
//是否為空
boolean blank = StrUtil.isBlank(str);//false
//是否不為空
boolean notBlank = StrUtil.isNotBlank(str);//true
//去掉字符串后綴(removeprefix:前綴)
String removeSuffix = StrUtil.removeSuffix("test.txt", ".txt");//test
//忽略大小寫去掉前綴(removeSuffixIgnoreCase:去掉后綴)
String removePrefixIgnoreCase = StrUtil.removePrefixIgnoreCase(str, "A");//bCDEfghi
//sub方法
//順數(shù)第2個到第4個,包含尾部包含頭
String sub = StrUtil.sub(str, 2, 4);//CD
//-3表示倒數(shù)第三個字符
String sub1 = StrUtil.sub(str, 2, -3);//CDEf
//format方法(使用字符串模板代替字符串拼接)
String template = "{}愛{}!{}";
String fin = StrUtil.format(template, "我", "JAVA","哈"); //我愛JAVA!哈
1. hasBlank、hasEmpty方法
就是給定一些字符串,如果一旦有空的就返回true,常用于判斷好多字段是否有空的(例如web表單數(shù)據(jù))。
這兩個方法的區(qū)別是hasEmpty只判斷是否為null或者空字符串(“”),hasBlank則會把不可見字符也算做空,isEmpty和isBlank同理。
sub方法
避免subString方法越界問題,index的位置還支持負數(shù)哦,-1表示最后一個字符(這個思想來自于Python),還有就是如果不小心把第一個位置和第二個位置搞反了,也會自動修正(例如想截取第4個和第2個字符之間的部分也是可以的
public static String sub(CharSequence str, int fromIndex, int toIndex)
public static String subPreGbk(CharSequence str, int len, CharSequence suffix)
public static String maxLength(CharSequence string, int length)
public static String subPre(CharSequence string, int toIndex)
public static String subSuf(CharSequence string, int fromIndex)
public static String subSufByLength(CharSequence string, int length)
public static String subWithLength(String input, int fromIndex, int length)
public static String subBefore(CharSequence string, CharSequence separator, boolean isLastSeparator)
public static String subBefore(CharSequence string, char separator, boolean isLastSeparator)
public static String subAfter(CharSequence string, CharSequence separator, boolean isLastSeparator)
public static String subAfter(CharSequence string, char separator, boolean isLastSeparator)
public static String subBetween(CharSequence str, CharSequence before, CharSequence after)
public static String subBetween(CharSequence str, CharSequence beforeAndAfter)
String str = “abcdefgh”;
String strSub1 = StrUtil.sub(str, 2, 3); //strSub1 -> c
String strSub2 = StrUtil.sub(str, 2, -3); //strSub2 -> cde
String strSub3 = StrUtil.sub(str, 3, 2); //strSub2 -> c
去空格 回車操作 與空有關(guān)的方法
public static boolean isBlank(CharSequence str)
public static boolean isBlankIfStr(Object obj)
public static boolean isNotBlank(CharSequence str)
public static boolean hasBlank(CharSequence… strs)
public static boolean isAllBlank(CharSequence… strs)
public static boolean isEmpty(CharSequence str)
public static boolean isEmptyIfStr(Object obj)
public static boolean isNotEmpty(CharSequence str)
public static String nullToEmpty(CharSequence str)
public static String nullToDefault(CharSequence str, String defaultStr)
public static String emptyToDefault(CharSequence str, String defaultStr)
public static String blankToDefault(CharSequence str, String defaultStr)
public static String emptyToNull(CharSequence str)
public static boolean hasEmpty(CharSequence… strs)
public static boolean isAllEmpty(CharSequence… strs)
public static boolean isNullOrUndefined(CharSequence str)
public static boolean isEmptyOrUndefined(CharSequence str)
public static boolean isBlankOrUndefined(CharSequence str)
public static String cleanBlank(CharSequence str)
// 去空格
StrUtil.cleanBlank
// 去\n\r
StrUtil.removeAllLineBreaks()
字符串包含關(guān)系
// 字符串中: 同時匹配 “小明” 和 “19歲” 這兩個字符才返回true文章來源:http://www.zghlxwxcb.cn/news/detail-534459.html
boolean isContain = StrUtil.containsAll(“我叫小明今年18歲職業(yè)java軟件工程師”, “小明”, “19歲”);文章來源地址http://www.zghlxwxcb.cn/news/detail-534459.html
public static boolean startWith(CharSequence str, char c)
public static boolean startWith(CharSequence str, CharSequence prefix, boolean isIgnoreCase)
public static boolean startWith(CharSequence str, CharSequence prefix)
public static boolean startWithIgnoreCase(CharSequence str, CharSequence prefix)
public static boolean startWithAny(CharSequence str, CharSequence... prefixes)
public static boolean endWith(CharSequence str, char c)
public static boolean endWith(CharSequence str, CharSequence suffix, boolean isIgnoreCase)
public static boolean endWith(CharSequence str, CharSequence suffix)
public static boolean endWithIgnoreCase(CharSequence str, CharSequence suffix)
public static boolean endWithAny(CharSequence str, CharSequence... suffixes)
public static boolean contains(CharSequence str, char searchChar)
public static boolean containsAny(CharSequence str, CharSequence... testStrs)
public static boolean containsAny(CharSequence str, char... testChars)
public static boolean containsBlank(CharSequence str)
public static String getContainsStr(CharSequence str, CharSequence... testStrs)
public static boolean containsIgnoreCase(CharSequence str, CharSequence testStr)
public static boolean containsAnyIgnoreCase(CharSequence str, CharSequence... testStrs)
public static String getContainsStrIgnoreCase(CharSequence str, CharSequence... testStrs)
頭尾的一些處理
public static String trim(CharSequence str)
public static void trim(String[] strs)
public static String trimToEmpty(CharSequence str)
public static String trimToNull(CharSequence str)
public static String trimStart(CharSequence str)
public static String trimEnd(CharSequence str)
public static String trim(CharSequence str, int mode)
public static String strip(CharSequence str, CharSequence prefixOrSuffix)
public static String strip(CharSequence str, CharSequence prefix, CharSequence suffix)
public static String stripIgnoreCase(CharSequence str, CharSequence prefixOrSuffix)
public static String stripIgnoreCase(CharSequence str, CharSequence prefix, CharSequence suffix)
public static String addPrefixIfNot(CharSequence str, CharSequence prefix)
public static String addSuffixIfNot(CharSequence str, CharSequence suffix)
public static boolean isSurround(CharSequence str, CharSequence prefix, CharSequence suffix)
public static boolean isSurround(CharSequence str, char prefix, char suffix)
刪除字符操作
public static String removeAll(CharSequence str, CharSequence strToRemove)
public static String removeAll(CharSequence str, char... chars)
public static String removeAllLineBreaks(CharSequence str)
public static String removePreAndLowerFirst(CharSequence str, int preLength)
public static String removePreAndLowerFirst(CharSequence str, CharSequence prefix)
public static String removePrefix(CharSequence str, CharSequence prefix)
public static String removePrefixIgnoreCase(CharSequence str, CharSequence prefix)
public static String removeSuffix(CharSequence str, CharSequence suffix)
public static String removeSufAndLowerFirst(CharSequence str, CharSequence suffix)
public static String removeSuffixIgnoreCase(CharSequence str, CharSequence suffix)
大小寫的轉(zhuǎn)換
public static String upperFirstAndAddPre(CharSequence str, String preString)
public static String upperFirst(CharSequence str)
public static String lowerFirst(CharSequence str)
public static boolean isUpperCase(CharSequence str)
public static boolean isLowerCase(CharSequence str)
分割操作
public static String[] splitToArray(CharSequence str, char separator)
public static long[] splitToLong(CharSequence str, char separator)
public static long[] splitToLong(CharSequence str, CharSequence separator)
public static int[] splitToInt(CharSequence str, char separator)
public static int[] splitToInt(CharSequence str, CharSequence separator)
public static List<String> split(CharSequence str, char separator)
public static String[] splitToArray(CharSequence str, char separator, int limit)
public static List<String> split(CharSequence str, char separator, int limit)
public static List<String> splitTrim(CharSequence str, char separator)
public static List<String> splitTrim(CharSequence str, CharSequence separator)
public static List<String> splitTrim(CharSequence str, char separator, int limit)
public static List<String> splitTrim(CharSequence str, CharSequence separator, int limit)
public static List<String> split(CharSequence str, char separator, boolean isTrim, boolean ignoreEmpty)
public static List<String> split(CharSequence str, char separator, int limit, boolean isTrim, boolean ignoreEmpty)
public static List<String> split(CharSequence str, CharSequence separator, int limit, boolean isTrim, boolean ignoreEmpty)
public static String[] split(CharSequence str, CharSequence separator)
public static String[] split(CharSequence str, int len)
public static String[] cut(CharSequence str, int partLength)
判斷相等
public static boolean equals(CharSequence str1, CharSequence str2)
public static boolean equalsIgnoreCase(CharSequence str1, CharSequence str2)
public static boolean equals(CharSequence str1, CharSequence str2, boolean ignoreCase)
public static boolean isSubEquals(CharSequence str1, int start1, CharSequence str2, int start2, int length, boolean ignoreCase)
public static boolean isAllCharMatch(CharSequence value, Matcher<Character> matcher)
public static boolean equalsCharAt(CharSequence str, int position, char c)
出現(xiàn)次數(shù)統(tǒng)計
public static int count(CharSequence content, CharSequence strForSearch)
public static int count(CharSequence content, char charForSearch)
到了這里,關(guān)于HuTool_字符串工具-StrUtil的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!