AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

超好用的Java常用工具類StringUtils(帶代碼實例),提升開發效率

來源: 責編: 時間:2023-10-16 17:09:04 230觀看
導讀工具類commons-lang3有很多好用的工具類,今天分享一下StringUtils中常用的方法,來提升我們的開發效率!添加依賴<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</

5T328資訊網——每日最新資訊28at.com

工具類commons-lang3有很多好用的工具類,今天分享一下StringUtils中常用的方法,來提升我們的開發效率!5T328資訊網——每日最新資訊28at.com

添加依賴

<dependency>            <groupId>org.apache.commons</groupId>            <artifactId>commons-lang3</artifactId>            <version>3.8.1</version>        </dependency>

isNotEmpty 非空判斷

StringUtils.isNotEmpty(str),用于字符串的非空判斷方法。5T328資訊網——每日最新資訊28at.com

使用示例:5T328資訊網——每日最新資訊28at.com

System.out.println(StringUtils.isNotEmpty(null)); // falseSystem.out.println(StringUtils.isNotEmpty("")); // falseSystem.out.println(StringUtils.isNotEmpty(" ")); // trueSystem.out.println(StringUtils.isNotEmpty("TnT")); // trueSystem.out.println(StringUtils.isNotEmpty("  TnT  ")); // true

5T328資訊網——每日最新資訊28at.com

isNotEmpty方法判斷邏輯5T328資訊網——每日最新資訊28at.com

String str ="";// 淘汰if (str != null && str != "") {}// 推薦if (StringUtils.isNotEmpty(str)) {}

isNotBlank 非空判斷

StringUtils.isNotBlank(str),用于字符串的非空判斷方法。5T328資訊網——每日最新資訊28at.com

  • isNotBlank對于空格和空字符串都是為false。
  • 而isNotEmpty對于空格為true,空字符串為false。

使用示例:5T328資訊網——每日最新資訊28at.com

System.out.println(StringUtils.isNotBlank(null)); // falseSystem.out.println(StringUtils.isNotBlank("")); // falseSystem.out.println(StringUtils.isNotBlank(" ")); // falseSystem.out.println(StringUtils.isNotBlank("TnT")); // trueSystem.out.println(StringUtils.isNotBlank("  TnT  ")); // true

5T328資訊網——每日最新資訊28at.com

isNotBlank方法判斷邏輯5T328資訊網——每日最新資訊28at.com

overlay 覆蓋

StringUtils.overlay(str, overlay, start, end),使用overlay內容覆蓋str。5T328資訊網——每日最新資訊28at.com

從0開始,位置從【start,end)左閉右開5T328資訊網——每日最新資訊28at.com

使用示例:5T328資訊網——每日最新資訊28at.com

// 用overlay覆蓋str,從start開始end-1結束的字符。System.out.println(StringUtils.overlay(null, null, 0, 0)); // nullSystem.out.println(StringUtils.overlay("", "abc", 0, 0)); // abcSystem.out.println(StringUtils.overlay("abcdef", null, 2, 4)); // abefSystem.out.println(StringUtils.overlay("abcdef", "", 2, 4)); // abefSystem.out.println(StringUtils.overlay("abcdef", "", 4, 2)); // abefSystem.out.println(StringUtils.overlay("abcdef", "", 4, 2)); // abefSystem.out.println(StringUtils.overlay("abcdef", "zzzz", 2, 4)); // abzzzzefSystem.out.println(StringUtils.overlay("abcdef", "zzzz", 4, 2)); // abzzzzefSystem.out.println(StringUtils.overlay("abcdef", "zzzz", -1, 4)); // zzzzefSystem.out.println(StringUtils.overlay("abcdef", "zzzz", 2, 8)); // abzzzzSystem.out.println(StringUtils.overlay("abcdef", "zzzz", -2, -3)); // zzzzabcdefSystem.out.println(StringUtils.overlay("abcdef", "zzzz", 8, 10)); // abcdefzzzz

5T328資訊網——每日最新資訊28at.com

overlay方法使用實例5T328資訊網——每日最新資訊28at.com

小功能:對用戶的身份證號碼年月日脫敏?5T328資訊網——每日最新資訊28at.com

System.out.println(StringUtils.overlay("342401200010011234","********",6,14));// 342401********1234

replace 替換

StringUtils.replace(text, searchString, replacement)。5T328資訊網——每日最新資訊28at.com

把text字符串里的searchString字符串,替換為replacement字符串,如果replacement為null,則直接返回text內容。5T328資訊網——每日最新資訊28at.com

使用示例:5T328資訊網——每日最新資訊28at.com

System.out.println(StringUtils.replace("aba", "a", "")); // bSystem.out.println(StringUtils.replace("aba", "a", "z")); // zbz// 如果replacement為null,則直接返回text內容。System.out.println(StringUtils.replace(null, null, null)); // nullSystem.out.println(StringUtils.replace("", null, null)); // ""System.out.println(StringUtils.replace("any", null, null)); // anySystem.out.println(StringUtils.replace("any", null, null)); // anySystem.out.println(StringUtils.replace("any", "", null)); // anySystem.out.println(StringUtils.replace("aba", "a", null)); // aba

5T328資訊網——每日最新資訊28at.com

replace方法使用實例5T328資訊網——每日最新資訊28at.com

replaceOnce 替換一次

StringUtils.replaceOnce(text, searchString, replacement),5T328資訊網——每日最新資訊28at.com

把第一次出現的,在text字符串里的searchString字符串,替換為replacement字符串,如果replacement為null,則直接返回text內容。5T328資訊網——每日最新資訊28at.com

使用示例:5T328資訊網——每日最新資訊28at.com

System.out.println(StringUtils.replaceOnce("aba", "a", "")); // baSystem.out.println(StringUtils.replaceOnce("aba", "a", "z")); // zba// 如果replacement為null,則直接返回text內容。System.out.println(StringUtils.replaceOnce(null, null, null)); // nullSystem.out.println(StringUtils.replaceOnce("", null, null)); // ""System.out.println(StringUtils.replaceOnce("any", null, null)); // anySystem.out.println(StringUtils.replaceOnce("any", null, null)); // anySystem.out.println(StringUtils.replaceOnce("any", "", null)); // anySystem.out.println(StringUtils.replaceOnce("aba", "a", null)); // aba

5T328資訊網——每日最新資訊28at.com

replaceOnce方法使用實例5T328資訊網——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-13606-0.html超好用的Java常用工具類StringUtils(帶代碼實例),提升開發效率

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: Redisson雜談,你學到了什么?

下一篇: 七個上熱搜的GitHub開源人臉識別項目

標簽:
  • 熱門焦點
Top