Some usage instances of the StringUtils utility class in Java

  • 2020-04-01 03:55:59
  • OfStack

The StringUtils method operates on objects of type java.lang.string, which is a complement to the string-type operations provided by the JDK, and is null safe (that is, if the input parameter String is null, it does not throw a NullPointerException, but handles it accordingly; for example, if the input is null, it returns null, etc., you can see the source code).  

With the exception of the constructor, there are over 130 methods in StringUtils that are static, so we can call stringutils.xxx ()

Here's a brief overview of some common methods:  

StringUtils class at org.apache.com mons. Lang. StringUtils package
String isEmpty(String STR) and isNotEmpty(String STR)


System.out.println(StringUtils.isEmpty(null));             // true
System.out.println(StringUtils.isEmpty(""));               // true
System.out.println(StringUtils.isEmpty("  "));             //False   Note: there is no white space processing & cake in the isEmpty here; < br / > System.out.println(StringUtils.isEmpty(" t n f r "));  // false
System.out.println(StringUtils.isEmpty(" b "));           // false
System.out.println(StringUtils.isEmpty("X-rapido"));       // false
System.out.println(StringUtils.isEmpty(" X "));            // false


System.out.println(StringUtils.isNotEmpty(null));              // false
System.out.println(StringUtils.isNotEmpty(""));                // false
System.out.println(StringUtils.isNotEmpty("  "));              //True   Note: the isNotEmpty here does not do any whitespace handling & NBSP; < br / > System.out.println(StringUtils.isNotEmpty(" t n f r "));   // true
System.out.println(StringUtils.isNotEmpty(" b "));            // true
System.out.println(StringUtils.isNotEmpty("X-rapido"));        // true
System.out.println(StringUtils.isNotEmpty(" X "));             // true

String isBlank(String STR) and isNotBlank(String STR)

Include whitespace


System.out.println(StringUtils.isBlank(null));            // true
System.out.println(StringUtils.isBlank(""));              // true
System.out.println(StringUtils.isBlank("  "));            // true  
System.out.println(StringUtils.isBlank(" t n f r ")); // true
System.out.println(StringUtils.isBlank(" b "));          // false
System.out.println(StringUtils.isBlank("X-rapido"));      // false
System.out.println(StringUtils.isBlank(" X "));           // false


System.out.println(StringUtils.isNotBlank(null));            // false
System.out.println(StringUtils.isNotBlank(""));              // false
System.out.println(StringUtils.isNotBlank("  "));            // false  
System.out.println(StringUtils.isNotBlank(" t n f r ")); // false
System.out.println(StringUtils.isNotBlank(" b "));          // true
System.out.println(StringUtils.isNotBlank("X-rapido"));      // true
System.out.println(StringUtils.isNotBlank(" X "));           // true

Trim (String STR) and trimToNull(String STR) and trimToEmpty(String STR)   


System.out.println(StringUtils.trim(null));             // null
System.out.println(StringUtils.trim(""));               // ""
System.out.println(StringUtils.trim("  "));             // ""  
System.out.println(StringUtils.trim(" t n f r "));  // ""
System.out.println(StringUtils.trim(" b "));           // ""
System.out.println(StringUtils.trim("X-rapido"));       // "X-rapido"
System.out.println(StringUtils.trim(" X "));            // "X"


System.out.println(StringUtils.trimToNull(null));             // null
System.out.println(StringUtils.trimToNull(""));               // null
System.out.println(StringUtils.trimToNull("  "));             // null 
System.out.println(StringUtils.trimToNull(" t n f r "));  // null
System.out.println(StringUtils.trimToNull(" b "));           // null
System.out.println(StringUtils.trimToNull("X-rapido"));       // "X-rapido"
System.out.println(StringUtils.trimToNull(" X "));            // "X"


System.out.println(StringUtils.trimToEmpty(null));          // ""
System.out.println(StringUtils.trimToEmpty(""));    // ""
System.out.println(StringUtils.trimToEmpty("  "));          // ""
System.out.println(StringUtils.trimToEmpty(" t n f r "));     // ""
System.out.println(StringUtils.trimToEmpty(" b "));     // ""
System.out.println(StringUtils.trimToEmpty(" bsss "));    // sss
System.out.println(StringUtils.trimToEmpty("X-rapido"));   // "X-rapido"
System.out.println(StringUtils.trimToEmpty(" X "));      // "X"

String strip(String STR) and stripToNull(String STR) and stripToEmpty(String STR)


System.out.println(StringUtils.strip(null));            // null
System.out.println(StringUtils.strip(""));      // ""
System.out.println(StringUtils.strip("  "));            // ""
System.out.println(StringUtils.strip(" t n f r "));     // ""
System.out.println(StringUtils.strip(" b "));       // ""
System.out.println(StringUtils.strip(" bsss "));      // sss
System.out.println(StringUtils.strip("X-rapido"));     // "X-rapido"
System.out.println(StringUtils.strip(" X "));        // "X"


System.out.println(StringUtils.stripToNull(null));            // null
System.out.println(StringUtils.stripToNull(""));      // null
System.out.println(StringUtils.stripToNull("  "));            // null
System.out.println(StringUtils.stripToNull(" t n f r "));       // null
System.out.println(StringUtils.stripToNull(" b "));       // ""
System.out.println(StringUtils.stripToNull(" bsss "));      // sss
System.out.println(StringUtils.stripToNull("X-rapido"));     // "X-rapido"
System.out.println(StringUtils.stripToNull(" X "));        // "X"


System.out.println(StringUtils.stripToEmpty(null));            // ""
System.out.println(StringUtils.stripToEmpty(""));      // ""
System.out.println(StringUtils.stripToEmpty("  "));            // ""
System.out.println(StringUtils.stripToEmpty(" t n f r "));      // ""
System.out.println(StringUtils.stripToEmpty(" b "));       // ""
System.out.println(StringUtils.stripToEmpty(" bsss "));      // sss
System.out.println(StringUtils.stripToEmpty("X-rapido"));     // "X-rapido"
System.out.println(StringUtils.stripToEmpty(" X "));        // "X"

String strip(String STR, String stripChars)
Remove the characters in stripChars on both ends of STR. STR returns itself if null or equal to "", or strip(String STR) if stripChars is null or "".

String stripStart(String STR, String stripChars)& PI;
Similar to 11, remove the stripChars character from the STR front.

String stripEnd(String STR, String stripChars)& PI;
Similar to 11, remove the stripChars character from the STR end.

String [] stripAll (String [] STRS)  
Strip (String STR) for each String in the String array and return.   Returns STRS itself if STRS is null or STRS length is 0.

String[] stripAll(String[] STRS, String stripChars)
Strip (String STR, String stripChars) for each String in the String array, and then return. Returns STRS itself if STRS is null or STRS length is 0

Boolean equals(String str1, String str2)& PI;  
  Compare two strings for equality and consider them equal if both are empty.

Boolean equalsIgnoreCase(String str1, String str2)
Comparing two strings is not case sensitive and is considered equal if both are empty.

Int indexOf(String STR, char searchChar)
Returns the position where the character searchChar first appears in the string STR. Returns -1 if the searchChar does not appear in STR, and -1& PI if STR is null or "";  

Int indexOf(String STR, char searchChar, int startPos)
Returns the position where the character searchChar first appears in the string STR, starting with startPos. Returns -1 if the searchChar does not appear in STR starting from startPos, and -1 if STR is null or ""

Int indexOf(String STR, String searchStr)
Returns the first occurrence of the string searchStr in the string STR. Returns -1 if the searchStr is null or null, 0 if the searchStr is "" and STR is not null, and -1 if the searchStr is not in STR

Int ordinalIndexOf(String STR, String searchStr, int ordinal)
Returns the position of the string searchStr at the first ordinal occurrence in the string STR. If STR =null or searchStr=null or ordinal< =0 returns negative 1


System.out.println(StringUtils.ordinalIndexOf(null, "a", 1));       // -1
System.out.println(StringUtils.ordinalIndexOf("a", null, 1));       // -1
System.out.println(StringUtils.ordinalIndexOf("", "", 1));       // 0
System.out.println(StringUtils.ordinalIndexOf("aabaabaa", "a", 1));  // 0
System.out.println(StringUtils.ordinalIndexOf("aabaabaa", "a", 2));  // 1
System.out.println(StringUtils.ordinalIndexOf("aabaabaa", "b", 1));  // 2
System.out.println(StringUtils.ordinalIndexOf("aabaabaa", "b", 2));  // 5
System.out.println(StringUtils.ordinalIndexOf("aabaabaa", "ab", 1)); // 1
System.out.println(StringUtils.ordinalIndexOf("aabaabaa", "ab", 2)); // 4
System.out.println(StringUtils.ordinalIndexOf("aabaabaa", "bc", 1)); // -1
System.out.println(StringUtils.ordinalIndexOf("aabaabaa", "", 1));   // 0
System.out.println(StringUtils.ordinalIndexOf(" aabaabaa ", "", 2)); // 0

Int indexOf(String STR, String searchStr, int startPos)
Returns the position where the string searchStr first appears in the string STR, starting with startPos.


System.out.println(StringUtils.indexOf(null, "a", 1));        // -1
System.out.println(StringUtils.indexOf("a", null, 1));        // -1
System.out.println(StringUtils.indexOf("", "", 1));        // 0
System.out.println(StringUtils.indexOf("aabaabaa", "a", 1));  // 1
System.out.println(StringUtils.indexOf("aabaabaa", "a", 2));  // 3
System.out.println(StringUtils.indexOf("aabaabaa", "b", 1));  // 2
System.out.println(StringUtils.indexOf("aabaabaa", "b", 2));  // 2
System.out.println(StringUtils.indexOf("aabaabaa", "ab", 1)); // 4
System.out.println(StringUtils.indexOf("aabaabaa", "ab", 2)); // 1
System.out.println(StringUtils.indexOf("aabaabaa", "bc", 1)); // -1
System.out.println(StringUtils.indexOf("aabaabaa", "", 1));   // 1
System.out.println(StringUtils.indexOf(" aabaabaa ", "", 2)); // 2

Int lastIndexOf(String STR, char searchChar)& PI;
Int indexOf(String STR, char searchChar)

Int lastIndexOf(String STR, char searchChar, int startPos)
Int indexOf(String STR, char searchChar, int startPos)

Int lastIndexOf(String STR, String searchStr)
Int indexOf(String STR, String searchStr)

Int lastIndexOf(String STR, String searchStr, int startPos)
Int indexOf(String STR, String searchStr, int startPos)


Related articles: