org.apache.commons.lang.StringUtils的常用方法

简介: org.apache.commons.lang.StringUtils的常用方法

org.apache.commons.lang.StringUtils是apache的commons-lang-x.x.jar下的包,里面包含很多字符串操作方法,


官网(http://commons.apache.org/proper/commons-lang/javadocs/api-release/index.html)介绍的常用方法如下:


public class StringUtils
extends Object


Operations on String that are null safe


  • IsEmpty/IsBlank - checks if a String contains text
  • Trim/Strip - removes leading and trailing whitespace
  • Equals/Compare - compares two strings null-safe
  • startsWith - check if a String starts with a prefix null-safe
  • endsWith - check if a String ends with a suffix null-safe
  • IndexOf/LastIndexOf/Contains - null-safe index-of checks
  • IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut - index-of any of a set of Strings
  • ContainsOnly/ContainsNone/ContainsAny - does String contains only/none/any of these characters
  • Substring/Left/Right/Mid - null-safe substring extractions
  • SubstringBefore/SubstringAfter/SubstringBetween - substring extraction relative to other strings
  • Split/Join - splits a String into an array of substrings and vice versa
  • Remove/Delete - removes part of a String
  • Replace/Overlay - Searches a String and replaces one String with another
  • Chomp/Chop - removes the last part of a String
  • AppendIfMissing - appends a suffix to the end of the String if not present
  • PrependIfMissing - prepends a prefix to the start of the String if not present
  • LeftPad/RightPad/Center/Repeat - pads a String
  • UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize - changes the case of a String
  • CountMatches - counts the number of occurrences of one String in another
  • IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable - checks the characters in a String
  • DefaultString - protects against a null input String
  • Rotate - rotate (circular shift) a String
  • Reverse/ReverseDelimited - reverses a String
  • Abbreviate - abbreviates a string using ellipsis or another given String
  • Difference - compares Strings and reports on their differences
  • LevenshteinDistance - the number of changes needed to change one String into another


部分方法的示例:

package com.led.test;
import org.apache.commons.lang.StringUtils;
public class Test3 {
    @SuppressWarnings("deprecation")
    public static void main(String[] args) {
        //找到2个字符串第一个出现不同的位置(1开始)
        String difference = StringUtils.difference("s123", "s13");
        System.out.println(difference);//3
        //判断2个字符串是否相等
        boolean equals = StringUtils.equals("s1", "s1");
        System.out.println(equals);//true
        //判断字符串里面是否含有特定字符串
        boolean b2 = StringUtils.contains("asd", "as");
        System.out.println(b2);//true
        //把数组的元素用:进行拼接
        String concatStr = StringUtils.join(new String[]{"dog", "cat", "monkey"},":");
        System.out.println(concatStr);//dog:cat:monkey
        //根据特定分隔符对字符串进行拆分
        String[] split = StringUtils.split("apple|xiaomi|dell|lenovo", "|");
        for (String s1 : split) {
            System.out.print(s1 + "、");//apple、xiaomi、dell、lenovo、
        }
        System.out.println();
        //所有单词首字母大写
        String capitaliseAllWords = StringUtils.capitaliseAllWords("today i will go to china");
        System.out.println(capitaliseAllWords);//Today I Will Go To China
        //统计某个字符串在字符串出现的次数
        int matchCount = StringUtils.countMatches("Happy Birthday to you", "o");
        System.out.println(matchCount);//2
        //必须要8位,不够的就拿0去字符串左边补
        String leftPad = StringUtils.leftPad("54", 8, "0");
        System.out.println(leftPad);//00000054
        //必须要8位,不够的就拿0去字符串右边补
        String rightPad = StringUtils.rightPad("54", 8, "0");
        System.out.println(rightPad);//54000000
        //判断字符串是否以特定字符串开头,区分大小写
        boolean startsWith = StringUtils.startsWith("GoodMorning", "go");
        System.out.println(startsWith);//false
        //判断字符串是否以特定字符串开头,区分大小写
        boolean endsWith = StringUtils.endsWith("GoodMorning", "ing");
        System.out.println(endsWith);//true
    }
}
相关文章
|
1月前
|
消息中间件 Ubuntu Java
在Ubuntu 18.04上安装Apache Kafka的方法
在Ubuntu 18.04上安装Apache Kafka的方法
101 0
|
1月前
|
Ubuntu 关系型数据库 MySQL
在 Ubuntu 14.04 服务器上使用 Apache 安装 Drupal 的方法
在 Ubuntu 14.04 服务器上使用 Apache 安装 Drupal 的方法
19 0
|
1月前
|
Ubuntu Java 应用服务中间件
在Ubuntu 16.04上安装Apache Tomcat 8的方法
在Ubuntu 16.04上安装Apache Tomcat 8的方法
15 0
|
1月前
|
安全 Java 应用服务中间件
在CentOS 7上安装Apache Tomcat 8的方法
在CentOS 7上安装Apache Tomcat 8的方法
63 0
|
1月前
|
Ubuntu 安全 Java
在Ubuntu 14.04上安装Apache Tomcat 8的方法
在Ubuntu 14.04上安装Apache Tomcat 8的方法
15 0
|
1月前
|
消息中间件 存储 Ubuntu
在Ubuntu 14.04上安装Apache Kafka的方法
在Ubuntu 14.04上安装Apache Kafka的方法
15 0
|
1月前
|
关系型数据库 MySQL Linux
在 CentOS 7 服务器上安装和保护 phpMyAdmin 与 Apache 的方法
在 CentOS 7 服务器上安装和保护 phpMyAdmin 与 Apache 的方法
43 0
|
1月前
|
Java 应用服务中间件 Apache
在 Debian 服务器上安装和配置 Apache Tomcat 的方法
在 Debian 服务器上安装和配置 Apache Tomcat 的方法
24 0
|
4月前
|
消息中间件 Kafka Apache
Apache Flink消费Kafka数据时,可以通过设置`StreamTask.setInvokingTaskNumber`方法来实现限流
Apache Flink消费Kafka数据时,可以通过设置`StreamTask.setInvokingTaskNumber`方法来实现限流
156 1
|
云安全 消息中间件 监控
Apache Log4j2 高危漏洞应急响应处置方法汇总整理
Apache Log4j2 高危漏洞应急响应处置方法汇总整理
737 0
Apache Log4j2 高危漏洞应急响应处置方法汇总整理