Article directory
- Basic usage of `trim() method` of String class & amp; source code analysis
-
- 1. Basic use of `trim() method`
-
- 1. A brief introduction to trim()
- 2. trim() use case
- 2. `trim() method` source code analysis
Basic use of the trim() method
of the String class & amp; source code analysis
1. trim() method
basic usage
1. A brief introduction to trim()
trim(): Remove the leading and trailing spaces of the String string.
Use as follows:
String str = "ss aa"; String newStr = str.trim(); //The value of newStr is "ss aa"
2, trim() use cases
public static void main(String[] args) {<!-- --> // 1. The string contains some space characters System.out.println("1. The string contains some space characters: "); System.out.println(" ss aa "); System.out.println(" ss aa ".trim()); System.out.println(" ss aa ".isEmpty()); System.out.println(" ss aa ".trim().isEmpty()); // 2. The string is full of space characters System.out.println("2, the string is full of space characters: "); System.out.println(" "); System.out.println(" ".trim()); System.out.println(" ".isEmpty()); System.out.println(" ".trim().isEmpty()); // 3. Null character System.out.println("3, Null character: "); System.out.println(""); System.out.println("".trim()); System.out.println("".isEmpty()); System.out.println("".trim().isEmpty()); }
Output result:
1. The string contains some space characters: ss aa ss aa false false 2. The string is full of space characters: false true 3. Null character: true true
2. trim() method
source code analysis
public final class String implements java.io.Serializable, Comparable<String>, CharSequence {<!-- --> /** The value is used for character storage. */ private final char value[]; // character array /** Cache the hash code for the string */ private int hash; // Default to 0 // hash code /** use serialVersionUID from JDK 1.0.2 for interoperability */ private static final long serialVersionUID = -6849794470754667710L; // serial number /** * Allocates a new {@code String} that contains characters from a subarray * of the character array argument. The {@code offset} argument is the * index of the first character of the subarray and the {@code count} * argument specifies the length of the subarray. The contents of the * subarray are copied; subsequent modification of the character array does * not affect the newly created string. * * @param value * Array that is the source of characters * * @param offset * The initial offset * * @param count * The length * * @throws IndexOutOfBoundsException * If the {@code offset} and {@code count} arguments index * characters outside the bounds of the {@code value} array */ public String(char value[], int offset, int count) {<!-- --> if (offset < 0) {<!-- --> throw new StringIndexOutOfBoundsException(offset); } if (count <= 0) {<!-- --> if (count < 0) {<!-- --> throw new StringIndexOutOfBoundsException(count); } if (offset <= value. length) {<!-- --> this.value = "".value; return; } } // Note: offset or count might be near -1>>>1. if (offset > value. length - count) {<!-- --> throw new StringIndexOutOfBoundsException(offset + count); } this.value = Arrays.copyOfRange(value, offset, offset + count); } /** * Returns a string that is a substring of this string. The * substring begins at the specified {@code beginIndex} and * extends to the character at index {@code endIndex - 1}. * Thus the length of the substring is {@code endIndex-beginIndex}. * <p> * Examples: * <blockquote><pre> * "hamburger".substring(4, 8) returns "urge" * "smiles".substring(1, 5) returns "miles" *
*
* @param beginIndex the beginning index, inclusive.
* @param endIndex the ending index, exclusive.
* @return the specified substring.
* @exception IndexOutOfBoundsException if the
* {@code beginIndex} is negative, or
* {@code endIndex} is larger than the length of
* this {@code String} object, or
* {@code beginIndex} is larger than
* {@code endIndex}.
*/
public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > value. length) {
throw new StringIndexOutOfBoundsException(endIndex);
}
int subLen = endIndex – beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return ((beginIndex == 0) & amp; & amp; (endIndex == value. length)) ? this
: new String(value, beginIndex, subLen);
}
\t
/**
* Returns a string whose value is this string, with any leading and trailing
* whitespace removed.
*
* If this {@code String} object represents an empty character
* sequence, or the first and last characters of character sequence
* represented by this {@code String} object both have codes
* greater than {@code ‘\u0020’} (the space character), then a
* reference to this {@code String} object is returned.
*
* Otherwise, if there is no character with a code greater than
* {@code ‘\u0020’} in the string, then a
* {@code String} object representing an empty string is
* returned.
*
* Otherwise, let k be the index of the first character in the
* string whose code is greater than {@code ‘\u0020’}, and let
* m be the index of the last character in the string whose code
* is greater than {@code ‘\u0020’}. A {@code String}
* object is returned, representing the substring of this string that
* begins with the character at index k and ends with the
* character at index m-that is, the result of
* {@code this. substring(k, m + 1)}.
*
* This method may be used to trim whitespace (as defined above) from
* the beginning and end of a string.
*
* @return A string whose value is this string, with any leading and trailing white
* space removed, or this string if it has no leading or
* trailing white space.
*/
public String trim() {
int len = value.length; // length of character array
int st = 0; // index of character array
char[] val = value; /* avoid getfield opcode */
while ((st < len) & amp; & amp; (val[st] <= ' ')) {
st + + ; // st is used as beginIndex in this method
}
while ((st < len) & amp; & amp; (val[len - 1] <= ' ')) {
len–; // len is used as endIndex in this method
}
return ((st > 0) || (len < value. length)) ? substring(st, len) : this;
}
}