Examples of some usage of the StringUtils tool class in Java

Source: Internet
Author: User

Examples of some usage of the StringUtils tool class in Java

This article mainly introduces some usage examples of StringUtils tool class in Java. This article focuses on the use of isEmpty and isBlank methods, as well as the use of trim, strip, and other methods, for more information, see

The operation object of the StringUtils method is java. lang. string-type objects are complementary to the String-type operation method provided by JDK, and are null safe (that is, if the input parameter String is null, NullPointerException is not thrown, but corresponding processing is performed, for example, if the input is null, the return value is also null. You can view the source code ).

In addition to the constringutils, there are more than 130 methods in StringUtils and they are all static, so we can call StringUtils. xxx () in this way ()

The following describes some common methods:

The StringUtils class is under the org. apache. commons. lang. StringUtils package.

String isEmpty (String str) and isNotEmpty (String str)

The Code is as follows:

System. out. println (StringUtils. isEmpty (null); // true

System. out. println (StringUtils. isEmpty (""); // true

System. out. println (StringUtils. isEmpty (""); // false note: isEmpty does not process spaces.

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

The Code is as follows:

System. out. println (StringUtils. isNotEmpty (null); // false

System. out. println (StringUtils. isNotEmpty (""); // false

System. out. println (StringUtils. isNotEmpty (""); // true Note: isNotEmpty does not process spaces.

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 space

The Code is as follows:

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

The Code is as follows:

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

String trim (String str), trimToNull (String str), and trimToEmpty (String str)

The Code is as follows:

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"

The Code is as follows:

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"

The Code is as follows:

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), stripToNull (String str), and stripToEmpty (String str)

The Code is as follows:

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"

The Code is as follows:

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"

The Code is as follows:

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 at both ends of str in stripChars. If str is null or equal to "", it is returned. If stripChars is null or "", strip (String str) is returned ).

String stripStart (String str, String stripChars)

Similar to '11', remove the character in stripChars at the front end of 'str.

String stripEnd (String str, String stripChars)

Similar to 11, remove the character at the end of str in stripChars.

String [] stripAll (String [] strs)

Strip (String str) each String in the String array, and then return. If strs is null or the strs length is 0, strs itself is returned.

String [] stripAll (String [] strs, String stripChars)

Strip (String str, String stripChars) each String in the String array, and then return. If strs is null or the strs length is 0, the strs itself is returned.

Boolean equals (String str1, String str2)

Checks whether two strings are equal. If both strings are null, they are considered equal.

Boolean inclusignorecase (String str1, String str2)

Compares the two strings to determine whether they are equal and case insensitive. If both strings are null, they are considered equal.

Int indexOf (String str, char searchChar)

Returns the position where the searchChar character first appears in the str string. If searchChar does not appear in str,-1 is returned. If str is null or "",-1 is returned.

Int indexOf (String str, char searchChar, int startPos)

Returns the position of the first occurrence of the searchChar character in str from startPos. If searchChar does not appear in str from startPos,-1 is returned. If str is null or "",-1 is returned.

Int indexOf (String str, String searchStr)

Returns the position where the string searchStr first appears in the string str. If str is null or searchStr is null,-1 is returned. If searchStr is "" And str is not null, 0 is returned. If searchStr is not in str,-1 is returned.

Int ordinalIndexOf (String str, String searchStr, int ordinal)

Returns the position where the searchStr string appears at the ordinal Time in the str string. If str = null or searchStr = null or ordinal <= 0,-1 is returned.

The Code is as follows:

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 from startPos.

The Code is as follows:

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)

The basic principle is the same as int indexOf (String str, char searchChar)

Int lastIndexOf (String str, char searchChar, int startPos)

The basic principle is the same as that of int indexOf (String str, char searchChar, int startPos)

Int lastIndexOf (String str, String searchStr)

The basic principle is the same as int indexOf (String str, String searchStr)

Int lastIndexOf (String str, String searchStr, int startPos)

The basic principle is the same as that of int indexOf (String str, String searchStr, int startPos)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.