• StringUtils工具类


    StringUtils源码,使用的是commons-lang3-3.1包。
    下载地址 http://commons.apache.org/lang/download_lang.cgi

    以下是StringUtils的各项用法
    1.空字符串检查
      使用函数:StringUtils.isBlank(testString)
      函数介绍: 当testString为空,长度为零或者仅由空白字符(whitespace)组成时,返回True;否则返回False
      例程:
        System.out.println(StringUtils.isBlank(""));//true
        System.out.println( StringUtils.isBlank(" ") );//true
        System.out.println( StringUtils.isBlank(null) );//true

      函数StringUtils.isNotBlank(testString)的功能与StringUtils.isBlank(testString)相反.

    2.清除空白字符(String.trim())
      使用函数: StringUtils.trimToNull(testString)
      函数介绍:清除掉testString首尾的空白字符,如果仅testString全由空白字符(whitespace)组成则返回null
      例程:
        System.out.println( StringUtils.trimToNull(" ") );//null
        System.out.println( StringUtils.trimToNull(" A Test ") );//A Test
        System.out.println( StringUtils.trimToNull(null) );//null
      注意:函数StringUtils.trim(testString)与StringUtils.trimToNull(testString)功能类似,但testString由空白字符(whitespace)组成时返回零长度字符串。

    3.截取字符串(String.subString())
      使用函数: StringUtils.abbreviate(testString,width)和StringUtils.abbreviate(testString,offset,width)
      函数介绍:在给定的width内取得testString的缩写,当testString的长度小于width则返回原字符串.
      例程:
        String test = "This is a test of the abbreviation.";
        System.out.println( StringUtils.abbreviate( test, 15 ) );//This is a te...
        System.out.println( StringUtils.abbreviate( test, 5,15 ) );//...is a test...
        System.out.println( StringUtils.abbreviate( "Test", 10 ) );//Test

    4.拆分字符串(String.split)
      使用函数: StringUtils.split(testString,splitChars,arrayLength)
      函数介绍:splitChars中可以包含一系列的字符串来劈分testString,并可以设定得到数组的长度.注意设定长度arrayLength和劈分字符串间有抵触关系,建议一般情况下不要设定长度.
      例程:
        String input = "A b,c.d|e";
        String input2 = "Pharmacy, basketball funky";
        String[] array1 = StringUtils.split( input, " ,.|");//{A,b,c,d,e}
        String[] array2 = StringUtils.split( input2, " ,", 2 );//{Pharmacy,basketball funky}

    5.查找嵌套字符串
      使用函数:StringUtils.substringBetween(testString,header,tail)
      函数介绍:在testString中取得header和tail之间的字符串。不存在则返回空
      例程:
        String htmlContent = "ABC1234ABC4567";
        System.out.println(StringUtils.substringBetween(htmlContent, "1234", "4567"));//ABC
        System.out.println(StringUtils.substringBetween(htmlContent, "12345", "4567"));//null


    6.去除尾部换行符
      使用函数:StringUtils.chomp(testString)
      函数介绍:去除testString尾部的换行符
      例程:
        String input = "Hello ";
        System.out.println( StringUtils.chomp( input ));//Hello
        String input2 = "Another test ";
        System.out.println( StringUtils.chomp( input2 ));//Another test

    7.重复字符串
      使用函数:StringUtils.repeat(repeatString,count)
      函数介绍:得到将repeatString重复count次后的字符串
      例程:
        System.out.println( StringUtils.repeat( "China ", 5));//China China China China China


      其他函数:
        StringUtils.center( testString, count,repeatString );把testString插入将repeatString重复多次后的字符串中间,得到总长为count的字符串
        StringUtils.center(String str, int size); 默认以空格填充
        StringUtils.leftPad(String str,int size); 左侧空格填充
        StringUtils.leftPad(String str,int size,String padStr);左侧字符串填充
        StringUtils.rightPad(String str,int size); 左侧空格填充
        StringUtils.rightPad(String str,int size,String padStr);左侧字符串填充

      例程:
        System.out.println( StringUtils.center( "China", 11,"*"));//***China***
        StringUtils.leftPad("abc", 10, "*");//*******abc


    8.颠倒字符串顺序
      使用函数:StringUtils.reverse(testString)
      函数介绍:得到testString中字符颠倒后的字符串
      例程:
        System.out.println( StringUtils.reverse("ABCDE"));//EDCBA

    9.判断字符串内容的类型
      函数介绍:
       StringUtils.isNumeric( testString ) :如果testString全由数字组成返回True
       StringUtils.isAlpha( testString ) :如果testString全由字母组成返回True
       StringUtils.isAlphanumeric( testString ) :如果testString全由数字或数字组成返回True
       StringUtils.isAlphaspace( testString ) :如果testString全由字母或空格组成返回True
       StringUtils.isAlphanumericSpace(String str);如果testString全由字母数字和空格组成返回True
       StringUtils.isNumericSpace(String str);如果testString全由数字和空格组成返回True

      例程:
        String state = "Virginia";
        System.out.println( StringUtils.isNumeric(state ) );//false
        System.out.println( StringUtils.isAlpha( state ) );//true
        System.out.println( StringUtils.isAlphanumeric( state ) );//true
        System.out.println( StringUtils.isAlphaSpace( state ) );//true

    10.取得某字符串在另一字符串中出现的次数
      使用函数:StringUtils.countMatches(testString,seqString)
      函数介绍:取得seqString在testString中出现的次数,未发现则返回零
      例程:
        System.out.println(StringUtils.countMatches( "Chinese People", "e" ));//4

    11.部分截取字符串
      使用函数:
       StringUtils.substringBetween(testString,fromString,toString ):取得两字符之间的字符串
       StringUtils.substringAfter( ):取得指定字符串后的字符串
       StringUtils.substringBefore( ):取得指定字符串之前的字符串
       StringUtils.substringBeforeLast( ):取得最后一个指定字符串之前的字符串
       StringUtils.substringAfterLast( ):取得最后一个指定字符串之后的字符串

      例程:
        String formatted = " 25 * (30,40) [50,60] | 30";
        System.out.print(StringUtils.substringBeforeLast( formatted, "*" ));//25
        System.out.print(StringUtils.substringBetween( formatted, "(", "," ));//30
        System.out.print(StringUtils.substringBetween( formatted, ",", ")" ));//40
        System.out.print(StringUtils.substringBetween( formatted, "[", "," ));//50
        System.out.print(StringUtils.substringBetween( formatted, ",", "]" ));//40) [50,60
        System.out.print(StringUtils.substringAfterLast( formatted, "|" ));//30


    12. 缩进字符串:
      StringUtils.abbreviate(String str, int maxWidth) 缩进字符串,第二个参数至少为4(包括...)
      例程:
        StringUtils.abbreviate("abcdefg", 20);//abcdefg (正常显示)
        StringUtils.abbreviate("abcdefg", 4);//a...

    13. 首字母大写:
      StringUtils.capitalize(String str) 首字母大写
      StringUtils.uncapitalize(String str)首字母小写
      例程:
        StringUtils.capitalize("abcdefg");//Abcdefg

    14. 是否全是大写,是否全是小写(3.0版本)
      StringUtils.isAllLowerCase(String str);//是否全是大写
      StringUtils.isAllUpperCase(String str);//是否全是小写
      例程:
        StringUtils.isAllLowerCase("abC");//false

    15. 大小写转换,空格不动
      StringUtils.swapCase(String str);//大小写转换
      例程:
        StringUtils.swapCase("I am a-A*a")//i AM A-a*A

  • 相关阅读:
    【Hadoop】MapReduce练习:多job关联实现倒排索引
    【Hadoop】MapReduce练习:分科目等级并按分区统计学生以及人数
    【Zookeeper】利用zookeeper搭建Hdoop HA高可用
    【Linux】部署NTP时间同步服务器
    VSCode前端文件以服务器模式打开
    移动端公共方法封装
    常用浏览器及内核
    XHTML和HTML的区别
    javascript算法
    计算属性和侦听器
  • 原文地址:https://www.cnblogs.com/zhaoyhBlog/p/6233032.html
Copyright © 2020-2023  润新知