scala stringUtils 详解

scala stringUtils 详解,第1张

scala stringUtils 详解 引用

Class StringUtils


    org.apache.commons
    commons-lang3
    3.8

import org.apache.commons.lang3.stringUtils

scala> val s1="1234567890"
s1: String = 1234567890

scala> val s2="abcdefghij"
s2: String = abcdefghij

a
scala> StringUtils.a
abbreviate   abbreviateMiddle   appendIfMissing   appendIfMissingIgnoreCase

abbreviate vs abbreviateMiddle

功能 字符串缩写abbreviate 缩写后的位数要大于3abbreviateMiddle 中间值填充 ,注意生效条件
源码

 public static String abbreviateMiddle(String str, String middle, int length) {
        if (!isEmpty(str) && !isEmpty(middle)) {
            if (length < str.length() && length >= middle.length() + 2) {
                int targetSting = length - middle.length();
                int startOffset = targetSting / 2 + targetSting % 2;
                int endOffset = str.length() - targetSting / 2;
                StringBuilder builder = new StringBuilder(length);
                builder.append(str.substring(0, startOffset));
                builder.append(middle);
                builder.append(str.substring(endOffset));
                return builder.toString();
            } else {
                return str;
            }
        } else {
            return str;
        }
    }

scala> StringUtils.abbreviate(s1,4)
res52: String = 1...

scala> StringUtils.abbreviate(s1,3)
java.lang.IllegalArgumentException: Minimum abbreviation width is 4
  at org.apache.commons.lang3.StringUtils.abbreviate(StringUtils.java:7365)
  at org.apache.commons.lang3.StringUtils.abbreviate(StringUtils.java:7322)
  ... 50 elided
  
scala> StringUtils.abbreviateMiddle(s1,"***",8)
res54: String = 123***90


appendIfMissing appendIfMissingIgnoreCase

是否以某字符串结尾,没有就append是否忽略大小写

scala> StringUtils.appendIfMissing(s2,"xyz")
res56: String = abcdefghijxyz

scala> StringUtils.appendIfMissing(s2,"hij")
res57: String = abcdefghij

//不忽略大小写
scala> StringUtils.appendIfMissing(s2,"HIJ")
res58: String = abcdefghijHIJ

// 忽略大小写
scala> StringUtils.appendIfMissingIgnoreCase(s2,"HIJ")
res59: String = abcdefghij
 
c
scala> StringUtils.c
capitalize   chomp   compare             contains      containsIgnoreCase   containsOnly         countMatches   
center       chop    compareIgnoreCase   containsAny   containsNone         containsWhitespace   
capitalize 首字母大写 chomp 切除后面的字符串
scala> StringUtils.chomp("123abc","abc")
warning: there was one deprecation warning; re-run with -deprecation for details
res119: String = 123

scala> StringUtils.chomp("123abc","ab")
warning: there was one deprecation warning; re-run with -deprecation for details
res120: String = 123abc

compare contains vs containsIgnoreCase vs containsAny

containsAny 字符串是否包含子字符串中任意的字符

scala> StringUtils.containsAny("abc123","123")
res122: Boolean = true

scala> StringUtils.containsAny("abc123","124")
res123: Boolean = true
containsWhitespace 是否包含空格、回车、换行符
scala> StringUtils.containsWhitespace("abc de")
res130: Boolean = true

scala> StringUtils.containsWhitespace("abcnde")
res131: Boolean = true

scala> StringUtils.containsWhitespace("abcrde")
res132: Boolean = true

d
scala>  StringUtils.d
defaultIfBlank   defaultIfEmpty   defaultString   deleteWhitespace   difference

defaultIfBlank vs defaultIfEmpty defaultString deleteWhitespace 删除字符中空白
scala> StringUtils.deleteWhitespace("abcrde")
res133: String = abcde

scala> StringUtils.deleteWhitespace("abcnde")
res134: String = abcde

scala> StringUtils.deleteWhitespace("abc nde")
res135: String = abcnde

difference e
scala>  StringUtils.e
endsWith   endsWithAny   endsWithIgnoreCase   equals   equalsAny   equalsAnyIgnoreCase   equalsIgnoreCase

endsWith

是否否以子字符串结尾 endsWithAny

是否以指定的一个或者多个字符串中的其中一个结尾 endsWithIgnoreCas equals vs equalsAny vs equalsAnyIgnoreCase vs equalsIgnoreCase g

scala>  StringUtils.get
getCommonPrefix   getFuzzyDistance   getJaroWinklerDistance   getLevenshteinDistance

getCommonPrefix getFuzzyDistance getJaroWinklerDistance getLevenshteinDistance i
scala>  StringUtils.i
indexOf             indexOfIgnoreCase   isAlphaSpace          isAnyEmpty         isNoneBlank   isNumeric        
indexOfAny          isAllLowerCase      isAlphanumeric        isAsciiPrintable   isNoneEmpty   isNumericSpace   
indexOfAnyBut       isAllUpperCase      isAlphanumericSpace   isBlank            isNotBlank    isWhitespace     
indexOfDifference   isAlpha             isAnyBlank            isEmpty            isNotEmpty 
j
scala>  StringUtils.join
join   joinWith
l
scala>  StringUtils.l
lastIndexOf   lastIndexOfAny   lastIndexOfIgnoreCase   lastOrdinalIndexOf   left   leftPad   length   lowerCase

leftPad vs rightPad

左右侧填充空格或者其他

scala> StringUtils.rightPad("123456",10)
res206: String = "123456    "

scala> StringUtils.rightPad("123456",10,"*")
res207: String = 123456****

scala> StringUtils.leftPad("123456",10,"*")
left   leftPad

scala> StringUtils.leftPad("123456",10,"*")
res208: String = ****123456

scala> StringUtils.leftPad("123456",10)
res209: String = "    123456"
left vs right

从左边或者右边截取指定长度的字符串

scala> StringUtils.right("123456abc",4)
res198: String = 6abc

scala> StringUtils.left("123456abc",4)
res199: String = 1234

length

返回字符串的长度 m

 StringUtils.mid

n
  StringUtils.normalizeSpace

o
scala>  StringUtils.o
ordinalIndexOf   overlay

p
scala>  StringUtils.prependIfMissing
prependIfMissing   prependIfMissingIgnoreCase
prependIfMissing vs prependIfMissingIgnoreCase

参考 append r

scala>  StringUtils.r
remove                removeIgnoreCase        replace                 replaceFirst            reverse            
removeAll             removePattern           replaceAll              replaceIgnoreCase       reverseDelimited   
removeEnd             removeStart             replaceChars            replaceonce             right              
removeEndIgnoreCase   removeStartIgnoreCase   replaceEach             replaceonceIgnoreCase   rightPad           
removeFirst           repeat                  replaceEachRepeatedly   replacePattern          rotate 

rotate

旋转字符串

scala> StringUtils.rotate("123456",1)
res201: String = 612345

scala> StringUtils.rotate("123456",-1)
res202: String = 234561

scala> StringUtils.rotate("123456",6)
res203: String = 123456

scala> StringUtils.rotate("123456",8)
res204: String = 561234
replaceEachRepeatedly repeat

重复字符串

scala> StringUtils.repeat("*!",4)
res200: String = *!*!*!*!

rightPad vs leftPad

左右侧填充空格或者其他

scala> StringUtils.rightPad("123456",10)
res206: String = "123456    "

scala> StringUtils.rightPad("123456",10,"*")
res207: String = 123456****

scala> StringUtils.leftPad("123456",10,"*")
left   leftPad

scala> StringUtils.leftPad("123456",10,"*")
res208: String = ****123456

scala> StringUtils.leftPad("123456",10)
res209: String = "    123456"
replace vs replaceIgnoreCase

搜索替换字符串是否忽略大小写

scala> StringUtils.replace
replace      replaceChars   replaceEachRepeatedly   replaceIgnoreCase   replaceonceIgnoreCase   
replaceAll   replaceEach    replaceFirst            replaceonce         replacePattern 
scala> StringUtils.replace("abc123abc123","abc","xyz")
res194: String = xyz123xyz123

scala> StringUtils.replace("abc123abc123","aBc","xyz")
res195: String = abc123abc123

scala> StringUtils.replaceIgnoreCase("abc123abc123","aBc","xyz")
res196: String = xyz123xyz123

replace First

搜索替换第一个字符串

scala> StringUtils.replaceFirst("abc123abc123","abc","xyz")
res197: String = xyz123abc123

replaceonce vs replaceonceIgnoreCase

只替换一次是否忽略大小写 replaceEach replacePattern replaceChars right vs left

从左边或者右边截取指定长度的字符串

scala> StringUtils.right("123456abc",4)
res198: String = 6abc

scala> StringUtils.left("123456abc",4)
res199: String = 1234

remove vs removeIgnoreCase
scala> StringUtils.remove
remove      removeEnd             removeFirst        removePattern   removeStartIgnoreCase   
removeAll   removeEndIgnoreCase   removeIgnoreCase   removeStart  

remove 删除字符串的某一部分removeIgnoreCase 删除字符串的某一部分 (忽略大小写)

scala> StringUtils.remove("123abc123ABC1234","123")
res187: String = abcABC4

removeStart vs removeEnd vs removeStartIgnoreCase vs removeEndIgnoreCase

删除开头或者结尾子串

scala> StringUtils.removeStart("123abc123ABC1234","123")
res191: String = abc123ABC1234

removeFirst

删除第一个符合的字符串

scala> StringUtils.removeFirst("123abc123ABC1234","abc")
res189: String = 123123ABC1234

removeAll removePattern reverse

反转字符串 replaceAll replaceIgnoreCase reverseDelimited s

scala>  StringUtils.s
split                                    splitPreserveAllTokens   stripAccents   stripTonull          substringBeforeLast   
splitByCharacterType                     startsWith               stripAll       substring            substringBetween      
splitByCharacterTypeCamelCase            startsWithAny            stripEnd       substringAfter       substringsBetween     
splitByWholeSeparator                    startsWithIgnoreCase     stripStart     substringAfterLast   swapCase              
splitByWholeSeparatorPreserveAllTokens   strip                    stripToEmpty   substringBefore                            


split

分割字符串

scala> StringUtils.split("1,2,3,45",",")
res165: Array[String] = Array(1, 2, 3, 45)

splitPreserveAllTokens stripAccents substringBeforeLast splitByCharacterType startsWith

是否以某字符串开始 stripAll substring substringBetween splitByWholeSeparator startsWithIgnoreCase substringAfterLas swapCase

大小写转换

scala> StringUtils.swapCase("abcABC")
res172: String = ABCabc
splitByWholeSeparatorPreserveAllTokens strip vs stripStart vs stripEnd

strip 去除字符串两边的字符串

scala> StringUtils.stripStart("123abc123","123")
res169: String = abc123

scala> StringUtils.stripEnd("123abc123","123")
res170: String = 123abc

scala> StringUtils.strip("123abc123","123")
res171: String = abc
stripTonull vs stripToEmpty

如果传入的参数为null ,则返回空字符或者null,其余功能和strip一致 substringBefore t

scala>  StringUtils.t
toEncodedString   toString   trim   trimToEmpty   trimTonull   truncate

toEncodedString trim

-去除字符串两边的空白

trimToEmpt

去除字符串两边的空白,如果字符串为null,则返回空字符 trimTonull

去除字符串两边的空白,如果字符串为空字符和null,则返回null

scala> StringUtils.trimTonull("")
res158: String = null

scala> StringUtils.trimToEmpty("")
res159: String = ""

scala> StringUtils.trim("")
res160: String = ""
truncate

截取字符串

scala> StringUtils.truncate("123456",3)
res163: String = 123

u
scala>  StringUtils.u
uncapitalize   upperCase
uncapitalize 首字母小写
scala> StringUtils.uncapitalize("ABC")
res115: String = aBC

upperCase 转成大写
scala> StringUtils.upperCase("ABCabc")
res117: String = ABCABC
w
scala>  StringUtils.wrap
wrap   wrapIfMissing

wrap vs wrapIfMissing

用指定字符串填充两边字符wrapIfMissing 没有的话填充,有的话忽略

scala> StringUtils.wrapIfMissing("**123*","**")
res113: String = **123***

scala> StringUtils.wrap("**123*","**")
res114: String = ****123***

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5717556.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存