Common methods of StringUtils tool class

Source: Internet
Author: User
Tags control characters

The action object of the StringUtils method is an object of type java.lang.String, complements the String type operation method provided by the JDK, and is null-safe (that is, if the input parameter string is NULL, it is not thrown nullpoin Terexception, but does the corresponding processing, for example, if the input is null, the return is also NULL, etc., you can see the source code).

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

The following is a brief introduction to some common methods:

1. public static Boolean isEmpty (String str)
The criterion for determining whether a string is empty or null is Str==null or str.length () ==0
The following is an example of whether StringUtils is empty:

Stringutils.isempty (NULL) = True
Stringutils.isempty ("") = True
Stringutils.isempty ("") = false//note in StringUtils Hollow lattice for non-empty processing
Stringutils.isempty ("") = False
Stringutils.isempty ("Bob") = False
Stringutils.isempty ("Bob") = False

2. public static Boolean isnotempty (String str)
Determines whether a string is non-null, equal to!isempty (string str)
Here's an example:

Stringutils.isnotempty (NULL) = False
Stringutils.isnotempty ("") = False
Stringutils.isnotempty ("") = True
Stringutils.isnotempty ("") = True
Stringutils.isnotempty ("Bob") = True
Stringutils.isnotempty ("Bob") = True

3. public static Boolean IsBlank (String str)
Determines whether a string is empty or has a length of 0 or is composed of a white space character (whitespace)
Here's an example:
Stringutils.isblank (NULL) = True
Stringutils.isblank ("") = True
Stringutils.isblank ("") = True
Stringutils.isblank ("") = True
Stringutils.isblank ("\ t \ \f \ r") = True//For tabs, line feeds, page breaks, and carriage returns

Stringutils.isblank ()//All knowledge is a blank character
Stringutils.isblank ("\b") = false//"\b" for word boundary character
Stringutils.isblank ("Bob") = False
Stringutils.isblank ("Bob") = False

4. public static Boolean Isnotblank (String str)
Determines whether a string is not empty and does not have a length of 0 and is not composed of a white space character (whitespace), which is equal to!isblank (string str)
Here's an example:

Stringutils.isnotblank (NULL) = False
Stringutils.isnotblank ("") = False
Stringutils.isnotblank ("") = False
Stringutils.isnotblank ("") = False
Stringutils.isnotblank ("\ t \ \f \ r") = False
Stringutils.isnotblank ("\b") = true
Stringutils.isnotblank ("Bob") = True
Stringutils.isnotblank ("Bob") = True

5. public static string trim (String str)
Remove the control character at both ends of the string (control characters, Char <= 32) and return NULL if the input is null
Here's an example:
Stringutils.trim (NULL) = NULL
Stringutils.trim ("") = ""
Stringutils.trim ("") = ""
Stringutils.trim ("\b \ \ \ \f \ r") = ""
Stringutils.trim ("\n\tss \b") = "ss"
Stringutils.trim ("D d dd") = "D d DD"
Stringutils.trim ("dd") = "DD"
Stringutils.trim ("dd") = "DD"

6. public static string Trimtonull (String str)
Removes the control character at both ends of the string (control characters, Char <= 32) and returns NULL if it becomes null or ""
Here's an example:
Stringutils.trimtonull (NULL) = NULL
Stringutils.trimtonull ("") = null
Stringutils.trimtonull ("") = null
Stringutils.trimtonull ("\b \ n \ \f \ r") = NULL
Stringutils.trimtonull ("\n\tss \b") = "ss"
Stringutils.trimtonull ("D d dd") = "D d DD"
Stringutils.trimtonull ("dd") = "DD"
Stringutils.trimtonull ("dd") = "DD"

7. public static string Trimtoempty (String str)
Removes the control character at both ends of the string (control characters, Char <= 32), or returns "" if it becomes null or ""
Here's an example:
Stringutils.trimtoempty (NULL) = ""
Stringutils.trimtoempty ("") = ""
Stringutils.trimtoempty ("") = ""
Stringutils.trimtoempty ("\b \ \ \ \f \ r") = ""
Stringutils.trimtoempty ("\n\tss \b") = "ss"
Stringutils.trimtoempty ("D d dd") = "D d DD"
Stringutils.trimtoempty ("dd") = "DD"
Stringutils.trimtoempty ("dd") = "DD"

8. public static string strip (String str)
Remove whitespace characters (whitespace) at both ends of the string, or null if input is NULL
The following is an example (note and trim () difference):
Stringutils.strip (NULL) = NULL
Stringutils.strip ("") = ""
Stringutils.strip ("") = ""
Stringutils.strip ("\b \ \ \ \f \ r") = "\b"
Stringutils.strip ("\n\tss \b") = "ss \b"
Stringutils.strip ("D d dd") = "D d DD"
Stringutils.strip ("dd") = "DD"
Stringutils.strip ("dd") = "DD"

9. public static string Striptonull (String str)
Remove whitespace character (whitespace) at both ends of the string, or null if it becomes null or ""
The following is an example (note and trimtonull () the difference):
Stringutils.striptonull (NULL) = NULL
Stringutils.striptonull ("") = null
Stringutils.striptonull ("") = null
Stringutils.striptonull ("\b \ \ \ \f \ r") = "\b"
Stringutils.striptonull ("\n\tss \b") = "ss \b"
Stringutils.striptonull ("D d dd") = "D d DD"
Stringutils.striptonull ("dd") = "DD"
Stringutils.striptonull ("dd") = "DD"

public static string Striptoempty (String str)
Removes the white space character (whitespace) at both ends of the string, or returns "" if it becomes null or ""
The following is an example (note and trimtoempty () the difference):
Stringutils.striptonull (NULL) = ""
Stringutils.striptonull ("") = ""
Stringutils.striptonull ("") = ""
Stringutils.striptonull ("\b \ \ \ \f \ r") = "\b"
Stringutils.striptonull ("\n\tss \b") = "ss \b"
Stringutils.striptonull ("D d dd") = "D d DD"
Stringutils.striptonull ("dd") = "DD"
Stringutils.striptonull ("dd") = "DD"

The following methods only describe their functions, no longer examples:
public static 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;
If stripchars is null or "", then strip (String str) is returned.

public static string Stripstart (String str, string stripchars)
Similar to 11, the characters in Stripchars are removed from the Str frontend.

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

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

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

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

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

public static int indexOf (String str, char SearchChar)
Returns the position of the first occurrence of the character SearchChar in the string str.
If SearchChar does not appear in STR, return-1,
If STR is null or "", it also returns-1

public static int indexOf (String str, char SearchChar, int startpos)
Returns the position of the first occurrence of the character SearchChar in the string str starting from Startpos.
If starting from Startpos SearchChar does not appear in STR then return-1,
If STR is null or "", it also returns-1

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

. public static int Ordinalindexof (String str, string searchstr, int ordinal)
Returns the position of the string searchstr in ordinal occurrences of the string str.
If Str=null or Searchstr=null or ordinal<=0 is returned-1
Example (* represents any string):
Stringutils.ordinalindexof (NULL, *, *) = 1
Stringutils.ordinalindexof (*, NULL, *) = 1
Stringutils.ordinalindexof ("", "", *) = 0
Stringutils.ordinalindexof ("Aabaabaa", "a", 1) = 0
Stringutils.ordinalindexof ("Aabaabaa", "a", 2) = 1
Stringutils.ordinalindexof ("Aabaabaa", "B", 1) = 2
Stringutils.ordinalindexof ("Aabaabaa", "B", 2) = 5
Stringutils.ordinalindexof ("Aabaabaa", "AB", 1) = 1
Stringutils.ordinalindexof ("Aabaabaa", "AB", 2) = 4
Stringutils.ordinalindexof ("Aabaabaa", "BC", 1) = 1
Stringutils.ordinalindexof ("Aabaabaa", "", 1) = 0
Stringutils.ordinalindexof ("Aabaabaa", "", 2) = 0

public static int indexOf (String str, string searchstr, int startpos)
Returns the position of the first occurrence of the string searchstr from Startpos in the string str.
Example (* represents any string):
Stringutils.indexof (NULL, *, *) = 1
Stringutils.indexof (*, NULL, *) = 1
Stringutils.indexof ("", "", 0) = 0
Stringutils.indexof ("Aabaabaa", "a", 0) = 0
Stringutils.indexof ("Aabaabaa", "B", 0) = 2
Stringutils.indexof ("Aabaabaa", "AB", 0) = 1
Stringutils.indexof ("Aabaabaa", "B", 3) = 5
Stringutils.indexof ("Aabaabaa", "B", 9) = 1
Stringutils.indexof ("Aabaabaa", "B",-1) = 2
Stringutils.indexof ("Aabaabaa", "", 2) = 2
Stringutils.indexof ("abc", "", 9) = 3

. public static int lastIndexOf (String str, char SearchChar)
Fundamentals with 18

public static int lastIndexOf (String str, char SearchChar, int startpos)
Fundamentals with 19

public static int lastIndexOf (String str, string searchstr)
Fundamentals with 20

public static int lastIndexOf (String str, string searchstr, int startpos)
Fundamentals with 22

Also attached:

Use of the split (string regex) method of string
If we need to split a string into a string array, it is usually implemented with split (string regex).

For example:

Java code
    1. String str = "AA,BB,CC,DD";
    2. string[] Strarray = Str.split (",");
    3. System.out.println (strarray.length);
    4. For (int i = 0; i < strarray.length; i++) {
    5. System.out.println (Strarray[i]);
    6. }

The result is:
4
Aa
Bb
Cc
Dd

If
String str = "AA.BB.CC.DD";
string[] Strarray = Str.split (".");

The result is: 0

Why is the result not what we think, because the argument string regex is a regular expression (regular expression) instead of a normal string, whereas "." has a special meaning in a regular expression that matches all individual characters. If we want to split that, we have to escape to ".", string[] Strarray = Str.split (".") modified to string[] Strarray = str.split ("\ \").
In addition, see the official website for the detailed API for StringUtils

Original: http://janwer.iteye.com/blog/148313

Common methods of StringUtils tool class

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.