StringUtils API(字符串工具类)

    技术2022-07-13  70

    Maven依赖:

    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.10</version> </dependency>

    API

    IsEmpty/IsBlank - checks if a String contains text 检查字符串是否有内容 blank: 空白的;(文件等的)空白处,空格; empty: 空的;空的东西

    String s = "This is a test statement"; String blank = " "; String empty = ""; System.out.println(StringUtils.isBlank(s)); //false System.out.println(StringUtils.isBlank(blank)); //true System.out.println(StringUtils.isBlank(empty)); //true System.out.println(StringUtils.isNotBlank(s)); //true System.out.println(StringUtils.isNotBlank(blank)); //false System.out.println(StringUtils.isNotBlank(empty)); //false System.out.println(StringUtils.isEmpty(s)); //flase System.out.println(StringUtils.isEmpty(blank)); //false System.out.println(StringUtils.isEmpty(empty)); //true System.out.println(StringUtils.isNotEmpty(s)); //true System.out.println(StringUtils.isNotEmpty(blank)); //true System.out.println(StringUtils.isNotEmpty(empty)); //false

    Trim/Strip - removes leading and trailing whitespace 删除字符串开头和结尾的空白符。 trim: 修剪;除去(不必要部分) strip: 剥去(外皮);从(某处)拿走所有东西

    String s = " This is a test statement "; String empty = ""; System.out.println(s.length());//28 System.out.println(StringUtils.trim(s)+"-"+s.length());//This is a test statement-28 System.out.println(StringUtils.strip(s)+"-"+s.length());//This is a test statement-28 System.out.println(StringUtils.stripToEmpty(empty)); // System.out.println(StringUtils.stripToNull(null)); //null System.out.println(StringUtils.trimToEmpty(empty)); // System.out.println(StringUtils.trimToNull(null)); //null

    Equals/Compare - compares two strings null-safe 比较两个字符串null安全。 quals()返回boolean compare()返回相差的字符数

    String s = "This is a test statement"; String s1 = "This is a test statement"; String empty = ""; String n = null; System.out.println(StringUtils.equals(s,s1)); //true System.out.println(StringUtils.compare(s,s1)); //0 System.out.println(StringUtils.equals(s,empty)); //false System.out.println(StringUtils.compare(s,empty)); //24 System.out.println(StringUtils.equals(s,n)); //false System.out.println(StringUtils.compare(s,n)); //1 System.out.println(StringUtils.equals(empty,n)); //false System.out.println(StringUtils.compare(empty,n)); //1

    startsWith - check if a String starts with a prefix null-safe 检查字符串是否以xx前缀开始 空安全

    String s = "This is a test statement"; System.out.println(StringUtils.startsWith(s,"t")); //false

    endsWith - check if a String ends with a suffix null-safe 检查字符串是否以xx后缀开始 空安全

    String s = "This is a test statement"; System.out.println(StringUtils.endsWith(s,"t")); //true

    IndexOf/LastIndexOf/Contains - null-safe index-of checks null安全的索引检查。

    String s = "This is a test statement"; System.out.println(StringUtils.indexOf(s,"i")); //2 System.out.println(StringUtils.lastIndexOf(s,"i")); //5 System.out.println(StringUtils.contains(s,"i")); //true

    IndexOfAny/LastIndexOfAny/IndexOfAnyBut - index-of any of a set of Strings 字符串集合索引检查。 返回数组中的元素最早出现的下标。

    String s = "This is a test statement"; System.out.println(StringUtils.indexOfAny(s,"e","i")); //2 System.out.println(StringUtils.lastIndexOfAny(s,"i","e")); //21 System.out.println(StringUtils.indexOfAnyBut(s,"T")); //1

    ContainsOnly/ContainsNone/ContainsAny - does String contains only/none/any of these characters 字符在字符串中出现一次或一次也没有出现。

    String s = "This is a test statement"; System.out.println(StringUtils.containsOnly(s,"is")); //false System.out.println(StringUtils.containsNone(s,"hello")); //fasle System.out.println(StringUtils.containsAny(s,"men")); //true

    Substring/Left/Right/Mid - null-safe substring extractions null安全 子串的提取。

    String s = "Thisisateststatement"; System.out.println(StringUtils.substring(s,1,6));//[1,6) hisis System.out.println(StringUtils.left(s,3)); //Thi System.out.println(StringUtils.right(s,3)); //ent System.out.println(StringUtils.mid(s,6,3)); //ate

    SubstringBefore/SubstringAfter/SubstringBetween - substring extraction relative to other strings 子串提取依赖其它字符串。

    String s = "Thisisateststatement"; System.out.println(StringUtils.substringBefore(s,"is")); //Th System.out.println(StringUtils.substringAfter(s,"is")); //isateststatement System.out.println(StringUtils.substringBetween(s,"te")); //ststa

    Split/Join - splits a String into an array of substrings and vice versa 将字符串分割成子字符串数组,反之亦然

    String s = "This is a"; String s1 = ""; String [] str = new String[]{"1","2","3"}; String[] split = StringUtils.split(s, " "); for (int i = 0; i < split.length; i++) { System.out.println(split[i]);//This is a } System.out.println(s1); System.out.println(StringUtils.join(str,"-"));//1-2-3

    Remove - removes part of a String 删除字符串的一部分

    String s = "This is a"; System.out.println(StringUtils.remove(s,"is"));//Th a System.out.println(s);//This is a

    Replace/Overlay - Searches a String and replaces one String with another 替换字符串的部分字符。

    String s = "This is a"; System.out.println(StringUtils.replace(s,"is","qaz"));//Thqaz qaz a a System.out.println(s);//This is a System.out.println(StringUtils.overlay(s,"is",0,4));//[0,4) is is a System.out.println(s);//This is a

    Chop - removes the last part of a String 删除字符串最后的字符。

    String s = "This isa"; System.out.println(StringUtils.chop(s));//This is System.out.println(s); //This isa

    AppendIfMissing - appends a suffix to the end of the String if not present 在字符串末尾追加后缀(如果不存在)

    String s = "This isa"; System.out.println(StringUtils.appendIfMissing(s,"a"));//This isa System.out.println(s); //This isa System.out.println(StringUtils.appendIfMissing(s,"s"));//This isas System.out.println(s); //This isa

    PrependIfMissing - prepends a prefix to the start of the String if not present 在字符串开头(如果不存在)前加上一个前缀

    String s = "This isa"; System.out.println(StringUtils.prependIfMissing(s,"T"));//This isa System.out.println(s); //This isa System.out.println(StringUtils.prependIfMissing(s,"s"));//sThis isa System.out.println(s); //This isa

    LeftPad/RightPad/Center/Repeat - pads a String 填补一个字符串

    String s = "101"; System.out.println(StringUtils.leftPad(s,8,"0"));//00000101 System.out.println(StringUtils.rightPad(s,8,"0"));//10100000 System.out.println(StringUtils.center(s,8,"@@@"));//@@101@@@ System.out.println(StringUtils.repeat(s,"---",3));//101---101---101

    UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize - changes the case of a String 改变字符串的大小写

    String s = "daW sWSV VfeFRE"; System.out.println(StringUtils.upperCase(s));//DAW SWSV VFEFRE System.out.println(StringUtils.lowerCase(s));//daw swsv vfefre System.out.println(StringUtils.swapCase(s));//DAw Swsv vFEfre System.out.println(StringUtils.capitalize(s));//首字母大写 DaW sWSV VfeFRE System.out.println(StringUtils.uncapitalize(s));//取消首字母大写 daW sWSV VfeFRE

    CountMatches - counts the number of occurrences of one String in another 计算一个字符串在另一个字符串中的出现次数

    String s = "this is a test statement"; System.out.println(StringUtils.countMatches(s,"s"));//4

    IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable - checks the characters in a String 检查字符串中的字符

    String s = "asds哈哈"; String s1 = " 2134"; String s2 = " "; String s3 = "\u007F"; //检查字符串是否只包含Unicode字母(所有汉字+英文字母,数字和符号大多不包括在内) System.out.println(StringUtils.isAlpha(s));//true //检查字符串是否只包含Unicode数字 System.out.println(StringUtils.isNumeric(s1));//false //检查字符串是否只包含空格 System.out.println(StringUtils.isWhitespace(s2));//true //检查字符串是否只包含ASCII可打印字符(95个可打印字符(0x20-0x7E)) System.out.println(StringUtils.isAsciiPrintable(s3));//false

    DefaultString - protects against a null input String 防止null输入字符串

    String s =null; System.out.println(StringUtils.defaultString(s,"default"));//default

    Rotate - rotate (circular shift) a String 旋转(循环移位)字符串

    String s ="abcdefg"; System.out.println(StringUtils.rotate(s,0));//abcdefg System.out.println(StringUtils.rotate(s,2));//fgabcde System.out.println(StringUtils.rotate(s,-2));//cdefgab System.out.println(StringUtils.rotate(s,7));//abcdefg System.out.println(StringUtils.rotate(s,-7));//abcdefg System.out.println(StringUtils.rotate(s,9));//fgabcde System.out.println(StringUtils.rotate(s,-9));//cdefgab

    Reverse/ReverseDelimited - reverses a String 反转字符串

    String s ="abcdefg"; System.out.println(StringUtils.reverse(s));//gfedcba System.out.println(StringUtils.reverseDelimited(s,'d'));//efgdabc

    Abbreviate - abbreviates a string using ellipsis or another given String 使用省略号或其他给定字符串缩写字符串

    String s ="据香港特区政府新闻网站消息"; System.out.println(StringUtils.abbreviate(s,"...",8));//据香港特区... Difference - compares Strings and reports on their differences 比较字符串和报告的差异 String s = "ab"; System.out.println(StringUtils.difference(s,"abxyz"));//xyz
    Processed: 0.019, SQL: 9