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

部分方法的示例:

 1 package com.led.test;
 2 
 3 import org.apache.commons.lang.StringUtils;
 4 
 5 public class Test3 {
 6     @SuppressWarnings("deprecation")
 7     public static void main(String[] args) {
 8         //找到2個字符串第一個出現不同的位置(1開始)
 9         String difference = StringUtils.difference("s123", "s13");
10         System.out.println(difference);//3
11         
12         //判斷2個字符串是否相等
13         boolean equals = StringUtils.equals("s1", "s1");
14         System.out.println(equals);//true
15 
16         //判斷字符串里面是否含有特定字符串
17         boolean b2 = StringUtils.contains("asd", "as");
18         System.out.println(b2);//true
19 
20         //把數組的元素用:進行拼接
21         String concatStr = StringUtils.join(new String[]{"dog", "cat", "monkey"},":");
22         System.out.println(concatStr);//dog:cat:monkey
23 
24         //根據特定分隔符對字符串進行拆分
25         String[] split = StringUtils.split("apple|xiaomi|dell|lenovo", "|");
26         for (String s1 : split) {
27             System.out.print(s1 + "、");//apple、xiaomi、dell、lenovo、
28         }
29         System.out.println();
30         
31         //所有單詞首字母大寫
32         String capitaliseAllWords = StringUtils.capitaliseAllWords("today i will go to china");
33         System.out.println(capitaliseAllWords);//Today I Will Go To China
34 
35         //統計某個字符串在字符串出現的次數
36         int matchCount = StringUtils.countMatches("Happy Birthday to you", "o");
37         System.out.println(matchCount);//2
38 
39         //必須要8位,不夠的就拿0去字符串左邊補 
40         String leftPad = StringUtils.leftPad("54", 8, "0");
41         System.out.println(leftPad);//00000054
42         
43         //必須要8位,不夠的就拿0去字符串右邊補 
44         String rightPad = StringUtils.rightPad("54", 8, "0");
45         System.out.println(rightPad);//54000000
46 
47         //判斷字符串是否以特定字符串開頭,區分大小寫
48         boolean startsWith = StringUtils.startsWith("GoodMorning", "go");
49         System.out.println(startsWith);//false
50         
51         //判斷字符串是否以特定字符串開頭,區分大小寫
52         boolean endsWith = StringUtils.endsWith("GoodMorning", "ing");
53         System.out.println(endsWith);//true
54 
55     }
56 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM