国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

HuTool_字符串工具-StrUtil

這篇具有很好參考價值的文章主要介紹了HuTool_字符串工具-StrUtil。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

這個工具的用處類似于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

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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • JAVA - 字符串工具類StringBuilder和StringBuffer

    JAVA - 字符串工具類StringBuilder和StringBuffer

    目錄 文章目錄 前言 二.常用方法演示 1.append()用于將指定的字符串添加到當前StringBuilder對象的末尾 2.delete():用于刪除StringBuilder對象中指定位置的字符。 ?3.insert():用于在指定位置插入指定字符串。 ?4.replace():用于替換指定位置的字符或字符串。 5.reverse():用于將StringBu

    2024年02月06日
    瀏覽(26)
  • Apache StringUtils:Java字符串處理工具類

    Apache StringUtils:Java字符串處理工具類

    在我們的代碼中經(jīng)常需要對字符串判空,截取字符串、轉(zhuǎn)換大小寫、分隔字符串、比較字符串、去掉多余空格、拼接字符串、使用正則表達式等等。如果只用 String 類提供的那些方法,我們需要手寫大量的額外代碼,不然容易出現(xiàn)各種異?!,F(xiàn)在有個好消息是:org.apache.commo

    2024年01月16日
    瀏覽(71)
  • Java工具類——json字符串格式化處理

    Java工具類——json字符串格式化處理

    在我們拿到一團未經(jīng)格式化的json字符串時,非常不方便查看,比如這樣 因此隨手寫了個工具類用來格式化json。注意,原json字符串必須語法無誤,并且不包含換行、空格、縮進等,否則會保留下來。 ok廢話不多說上代碼 運行后效果

    2024年01月17日
    瀏覽(29)
  • 使用java.security.SecureRandom安全生成隨機數(shù)和隨機字符串工具類

    ????????在Java中,可以使用java.security.SecureRandom和java.util.Random類來生成隨機數(shù),但是它們之間有以下區(qū)別: ? ? ? ? 1、隨機性強度: SecureRandom 類提供了更高的隨機性強度。它使用了更安全的算法和種子生成器,以提供更高質(zhì)量的隨機數(shù)。這對于需要高度安全性的應(yīng)用程

    2024年04月26日
    瀏覽(27)
  • 根據(jù)Json字符串設(shè)計對應(yīng)的Java對象(使用fastjson和JSONUtil工具類驗證)

    記錄 :467 場景 :根據(jù)Json字符串設(shè)計對應(yīng)的Java對象。使用fastjson的JSON和hutool的JSONUtil將Java對象轉(zhuǎn)換為Json字符串驗證。 版本 :JDK 1.8,Spring?Boot 2.6.3,fastjson-2.0.33,hutool-all-5.8.12。 1.Json字符串 根據(jù)如下Json字符串設(shè)計Java對象。 2.對應(yīng)Java對象 2.1Java對象ProvinceDto (1)示例代碼 (2)解析代

    2024年02月11日
    瀏覽(28)
  • <Java工具類>json字符串、List Map,List 對象轉(zhuǎn)換為相應(yīng)的JavaBean對象

    依賴: 工具類(直接上代碼): 使用場景: (1).使用泛型方法:把json字符串轉(zhuǎn)換為相應(yīng)的JavaBean對象 ; 一般用于:Controller層: 例如: (2).List Map轉(zhuǎn)換List 對象:如List,將第二個參數(shù)傳遞為Student對象; (3).List 對象轉(zhuǎn)換List Map:

    2024年02月12日
    瀏覽(29)
  • java 字符串中插入字符串

    一、使用 StringBuilder 進行字符串處理,效率最高 輸出:?aaaa123abbbbcccc 效率比較高,整個過程只會產(chǎn)生2個對象 二、直接對字符串進行處理 此方法效率比較低,整個過程會產(chǎn)生4個對象 三、直接對字符串進行處理 將字符串轉(zhuǎn)成數(shù)組,然后對數(shù)組進行處理,不推薦,這里就不展

    2024年02月16日
    瀏覽(24)
  • 字符串隨機生成工具(開源)-Kimen(奇門)

    ????????由于最近筆者在開發(fā)數(shù)據(jù)脫敏相關(guān)功能,其中一類脫敏需求為能夠按照指定的格式隨機生成一個字符串來代替原有信息,數(shù)據(jù)看起來格式需要與原數(shù)據(jù)相同,如:電話號碼,身份證號以及郵箱等。在網(wǎng)上搜索了下,發(fā)現(xiàn)沒有特別合適的開源工具,于是秉承著沒有開

    2024年02月19日
    瀏覽(28)
  • Java分割字符串,分割逗號字符串,分割空格

    Java分割字符串,分割逗號字符串,分割空格

    在一些讀取文件,然后對數(shù)據(jù)進行處理的過程中,我們需要獲取到對應(yīng)的數(shù)據(jù),這里我有一個文本其中的數(shù)據(jù)都是用逗號進行隔開的,我需要獲取到他們;

    2024年02月11日
    瀏覽(38)
  • Java判斷一個字符串是否包含某個字符串

    Java判斷一個字符串是否包含某個字符串

    開發(fā)過程中,有時會判斷一個字符串是否包含某個字符串的操作,這里總結(jié)判斷方法。 方式一:contains()方法 ? ? ? ? 理解:contains() 方法用于判斷字符串中是否包含指定的字符或字符串。(判斷一個字符串是否包含某個字符串) ? ? ? ? 語法:public boolean contains(CharSequence

    2024年02月13日
    瀏覽(43)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包