Class StringUtils
org.apache.commons commons-lang33.8
import org.apache.commons.lang3.stringUtils
scala> val s1="1234567890" s1: String = 1234567890 scala> val s2="abcdefghij" s2: String = abcdefghija
scala> StringUtils.a abbreviate abbreviateMiddle appendIfMissing appendIfMissingIgnoreCaseabbreviate 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***90appendIfMissing appendIfMissingIgnoreCase
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 = abcdefghijc
scala> StringUtils.c capitalize chomp compare contains containsIgnoreCase containsOnly countMatches center chop compareIgnoreCase containsAny containsNone containsWhitespacecapitalize 首字母大写 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 = 123abccompare contains vs containsIgnoreCase vs containsAny
containsAny 字符串是否包含子字符串中任意的字符
scala> StringUtils.containsAny("abc123","123") res122: Boolean = true scala> StringUtils.containsAny("abc123","124") res123: Boolean = truecontainsWhitespace 是否包含空格、回车、换行符
scala> StringUtils.containsWhitespace("abc de") res130: Boolean = true scala> StringUtils.containsWhitespace("abcnde") res131: Boolean = true scala> StringUtils.containsWhitespace("abcrde") res132: Boolean = trued
scala> StringUtils.d defaultIfBlank defaultIfEmpty defaultString deleteWhitespace differencedefaultIfBlank 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 = abcndedifference e
scala> StringUtils.e endsWith endsWithAny endsWithIgnoreCase equals equalsAny equalsAnyIgnoreCase equalsIgnoreCaseendsWith
是否否以子字符串结尾 endsWithAny
是否以指定的一个或者多个字符串中的其中一个结尾 endsWithIgnoreCas equals vs equalsAny vs equalsAnyIgnoreCase vs equalsIgnoreCase g
scala> StringUtils.get getCommonPrefix getFuzzyDistance getJaroWinklerDistance getLevenshteinDistancegetCommonPrefix 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 isNotEmptyj
scala> StringUtils.join join joinWithl
scala> StringUtils.l lastIndexOf lastIndexOfAny lastIndexOfIgnoreCase lastOrdinalIndexOf left leftPad length lowerCaseleftPad 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 = 1234length
返回字符串的长度 m
StringUtils.midn
StringUtils.normalizeSpaceo
scala> StringUtils.o ordinalIndexOf overlayp
scala> StringUtils.prependIfMissing prependIfMissing prependIfMissingIgnoreCaseprependIfMissing 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 rotaterotate
旋转字符串
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 = 561234replaceEachRepeatedly 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 = xyz123xyz123replace First
搜索替换第一个字符串
scala> StringUtils.replaceFirst("abc123abc123","abc","xyz") res197: String = xyz123abc123replaceonce vs replaceonceIgnoreCase
只替换一次是否忽略大小写 replaceEach replacePattern replaceChars right vs left
从左边或者右边截取指定长度的字符串
scala> StringUtils.right("123456abc",4) res198: String = 6abc scala> StringUtils.left("123456abc",4) res199: String = 1234remove vs removeIgnoreCase
scala> StringUtils.remove remove removeEnd removeFirst removePattern removeStartIgnoreCase removeAll removeEndIgnoreCase removeIgnoreCase removeStart
remove 删除字符串的某一部分removeIgnoreCase 删除字符串的某一部分 (忽略大小写)
scala> StringUtils.remove("123abc123ABC1234","123") res187: String = abcABC4removeStart vs removeEnd vs removeStartIgnoreCase vs removeEndIgnoreCase
删除开头或者结尾子串
scala> StringUtils.removeStart("123abc123ABC1234","123") res191: String = abc123ABC1234removeFirst
删除第一个符合的字符串
scala> StringUtils.removeFirst("123abc123ABC1234","abc") res189: String = 123123ABC1234removeAll 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 substringBeforesplit
分割字符串
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 = ABCabcsplitByWholeSeparatorPreserveAllTokens 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 = abcstripTonull vs stripToEmpty
如果传入的参数为null ,则返回空字符或者null,其余功能和strip一致 substringBefore t
scala> StringUtils.t toEncodedString toString trim trimToEmpty trimTonull truncatetoEncodedString 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 = 123u
scala> StringUtils.u uncapitalize upperCaseuncapitalize 首字母小写
scala> StringUtils.uncapitalize("ABC") res115: String = aBCupperCase 转成大写
scala> StringUtils.upperCase("ABCabc") res117: String = ABCABCw
scala> StringUtils.wrap wrap wrapIfMissingwrap vs wrapIfMissing
用指定字符串填充两边字符wrapIfMissing 没有的话填充,有的话忽略
scala> StringUtils.wrapIfMissing("**123*","**") res113: String = **123*** scala> StringUtils.wrap("**123*","**") res114: String = ****123***
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)