Some usages of stringutils tool classes in Java _java

Source: Internet
Author: User
Tags trim

The action object of the StringUtils method is an object of type java.lang.String, supplemented by the JDK-supplied string manipulation method, and is null-safe (that is, if the input parameter String is NULL, it is not thrown nullpointe Rexception, but do the corresponding processing, for example, if the input is NULL, return is NULL, etc., you can view the source code.

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

Here are some common methods to do a brief introduction:

StringUtils class under the Org.apache.commons.lang.StringUtils package
string IsEmpty (String str) and Isnotempty (String str)

Copy Code code as follows:

SYSTEM.OUT.PRINTLN (Stringutils.isempty (null)); True
System.out.println (Stringutils.isempty ("")); True
System.out.println (Stringutils.isempty ("")); False Note: The IsEmpty here is not a short deal
System.out.println (Stringutils.isempty ("\ 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

Copy Code code as follows:

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 have a short handle
System.out.println (Stringutils.isnotempty ("\ 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)

Contains a space to

Copy Code code 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 ("\ 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

Copy Code code 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 ("\ 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) and Trimtonull (String str) and Trimtoempty (String str)

Copy Code code as follows:

SYSTEM.OUT.PRINTLN (Stringutils.trim (null)); Null
System.out.println (Stringutils.trim ("")); // ""
System.out.println (Stringutils.trim ("")); // ""
System.out.println (Stringutils.trim ("\ 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

Copy Code code 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 ("\ 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

Copy Code code as follows:

SYSTEM.OUT.PRINTLN (Stringutils.trimtoempty (null)); // ""
System.out.println (Stringutils.trimtoempty ("")); // ""
System.out.println (Stringutils.trimtoempty ("")); // ""
System.out.println (Stringutils.trimtoempty ("\ 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)

Copy Code code as follows:

SYSTEM.OUT.PRINTLN (Stringutils.strip (null)); Null
System.out.println (Stringutils.strip ("")); // ""
System.out.println (Stringutils.strip ("")); // ""
System.out.println (Stringutils.strip ("\ 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

Copy Code code 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 ("\ 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

Copy Code code as follows:

SYSTEM.OUT.PRINTLN (Stringutils.striptoempty (null)); // ""
System.out.println (Stringutils.striptoempty ("")); // ""
System.out.println (Stringutils.striptoempty ("")); // ""
System.out.println (Stringutils.striptoempty ("\ 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 the Stripchars at both ends of Str. If STR is null or equals "", it returns itself, or strip (String str) If stripchars is null or "".

String Stripstart (String str, string stripchars)
Similar to 11, remove the characters from the Stripchars in the Str front end.

String Stripend (String str, string stripchars)
Similar to 11, remove the characters in the Stripchars at the end of the Str.

String[] Stripall (string[] strs)
Strip (string str) for each string in the string array, and then return. If the STRs is null or the STRs length is 0, the STRs itself is returned.

String[] Stripall (string[] STRs, String stripchars)
Strip (String str, string stripchars) for each string in the string array, and then return. If STRs is null or STRs length is 0, then the STRs itself is returned

Boolean Equals (String str1, String str2)
Compares two strings for equality and is considered equal if all two are empty.

Boolean equalsignorecase (String str1, String str2)
Compares two strings for equality, is case-insensitive, and is considered equal if all two are empty.

int indexOf (String str, char SearchChar)
Returns the position of the first occurrence of the character SearchChar in string str. Returns-1 if the SearchChar does not appear in Str, or 1 if STR is null or "".

int indexOf (String str, char SearchChar, int startpos)
Returns the position of the first occurrence of the character SearchChar from startpos in string str. Returns-1 if STR is null or "" If the SearchChar does not appear in STR, and then returns-1 startpos

int indexOf (String str, string searchstr)
Returns the position of the first occurrence of the string searchstr in string str. Returns 1 if STR is NULL or SEARCHSTR null, if Searchstr is "" and STR is not NULL, returns 0 if SEARCHSTR is not in Str, returns 1

int Ordinalindexof (String str, string searchstr, int ordinal)
Returns the position of the string searchstr at the ordinal occurrence of the string str. If Str=null or Searchstr=null or ordinal<=0 returns-1

Copy Code code 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 of the string searchstr the first occurrence of string str, starting with startpos.

Copy Code code 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)
Basic principle with int indexOf (String str, char SearchChar)

int LastIndexOf (String str, char SearchChar, int startpos)
Basic principle with int indexOf (String str, char SearchChar, int startpos)

int LastIndexOf (String str, string searchstr)
Basic principle with int indexOf (String str, string searchstr)

int LastIndexOf (String str, string searchstr, int startpos)
Basic principle with 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.