String description, string

Source: Internet
Author: User

String description, string

During development, we will frequently use the String class. It is essential to master the implementation and common methods of String. Of course, we also need to understand its internal implementation.

I. Implementation of String

In Java, a char array is used to implement the String type. This char array is defined as the final type, which means that once a String is created, it is immutable. In addition, a hash of the int type is defined to save the hash value of the String.

 /** The value is used for character storage. */    private final char value[];    /** Cache the hash code for the string */    private int hash; // Default to 0
Ii. constructor in String

There are many methods to create a String, and there are also multiple constructors. However, the objective is to assign values to the value array. The parameters passed in the constructor can be roughly divided into several parts:

Src: source, which is the String to be saved in the value. The input values can be String, char, int, byte, Stringbuffer, StringBuilder, and boolean values.

Offset: Start position of the string in src.

Count: the number of strings assigned to the value in src.

Charset: Specifies the character set.

Of course, not every constructor needs these parameters, and we do not need to master them in detail one by one. As long as we know which constructor methods exist, we can query APIs when using them.

3. common methods and implementations in String 1. Getting Character Arrays

How to obtain the character array:

     public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)

This method copies the characters at the specified position in String to dst. The specific implementation is as follows:

Public void getChars (int srcBegin, int srcEnd, char dst [], int dstBegin ){
// Determine the specified position if (srcBegin <0) {throw new StringIndexOutOfBoundsException (srcBegin);} if (srcEnd> value. length) {throw new StringIndexOutOfBoundsException (srcEnd);} if (srcBegin> srcEnd) {throw new StringIndexOutOfBoundsException (srcEnd-srcBegin );}
// Call the native method to copy the array System. arraycopy (value, srcBegin, dst, dstBegin, srcEnd-srcBegin );}
2. equals (), inclusignorecase (), regionMatches (), compareTo (), compareToIgnoreCase (), hashCode ()

The equals () method is used to determine whether two strings are equal. The implementation logic is as follows:

Public boolean equals (Object anObject) {if (this = anObject) {// if two strings reference the same String Object, return true;} if (anObject instanceof String) {// otherwise, compare String anotherString = (String) anObject; int n = value from the first character on the premise that the length is equal. length; if (n = anotherString. value. length) {char v1 [] = value; char v2 [] = anotherString. value; int I = 0; while (n --! = 0) {if (v1 [I]! = V2 [I]) return false; I ++;} return true ;}} return false ;}

When signorecase () is used to ignore case sensitivity, the regionMatches () method is called internally.

The regionMatches () method is used to determine whether the two string regions are equal. There are two regionMatches () methods in String. The difference is that a boolean value is added to determine whether to ignore the case. The specific implementation is as follows:

Public boolean regionMatches (boolean ignoreCase, int toffset, String other, int ooffset, int len) {char ta [] = value; int to = toffset; char pa [] = other. value; int po = ooffset; if (ooffset <0) | (toffset <0) | (toffset> (long) value. length-len) | (ooffset> (long) other. value. length-len) {return false; // if the preceding conditions are not met, false is returned.} while (len --> 0) {char c1 = ta [to ++]; char c2 = pa [po ++]; if (C1 = c2) {continue;} if (ignoreCase) {// case sensitive. Char u1 = Character. toUpperCase (c1); char u2 = Character. toUpperCase (c2); if (u1 = u2) {continue;} if (Character. toLowerCase (u1) = Character. toLowerCase (u2) {continue;} return false;} return true ;}

In addition, there is also a method to determine whether two String zones are equal, but they are case sensitive: public boolean regionMatches (int toffset, String other, int ooffset, int len ), however, the above method is not reused in the internal implementation of this method.

The compareTo () method is used to compare the size of two strings. The comparison rule is: compare two strings in alphabetical order. This comparison is based on the Unicode values of each character in the string. Set thisStringThe character sequence represented by the object is compared with the character sequence represented by the parameter string. In the Lexicographic OrderStringThe object is compared to a negative integer before the parameter string. In the Lexicographic OrderStringThe object is located after the parameter string, and the comparison result is a positive integer. If the two strings are equal, the result is 0.. The specific implementation is as follows:  

Public int compareTo (String anotherString) {int len1 = value. length; int len2 = anotherString. value. length; int lim = Math. min (len1, len2); char v1 [] = value; char v2 [] = anotherString. value; int k = 0; while (k <lim) {char c1 = v1 [k]; char c2 = v2 [k]; if (c1! = C2) {return c1-c2; // returns the Unicode difference value. } K ++;} return len1-len2; // returns the length difference between the two strings. If the value is 0, the two strings are of the same size. }

CompareToIgnoreCase () method implementation: String internally customizes a class named CaseInsensitiveComparator to implement Comparator, which is used to compare two strings that are case-insensitive. The comparison logic is, take two characters in sequence for case-insensitive comparison. The other logic is similar to the above.

The hashCode () method returns the String hash value.

3. startWith (), endWith ()

StartsWith (String prefix) checks whether a String starts with a specified substring and returns a boolean value.

StartsWith (String prefix, int toffset) is used to determine whether the String starts from the specified position and returns a boolean value. The implementation logic is to extract and judge the two characters of the location.

EndsWith (String suffix) is used to determine whether a String ends with a certain String. Its implementation logic is to call the startsWith (String prefix, int toffset) method, the specific implementation is as follows:

 public boolean endsWith(String suffix) {        return startsWith(suffix, value.length - suffix.value.length);    }
4. indexOf (), lastIndexOf ()

IndexOf (int ch): returns the index of the first occurrence of the specified character in this string.

IndexOf (int ch, int fromIndex): searches for the specified index and returns the index of the specified character for the first time in this string.

LastIndexOf (int ch): returns the index of the last occurrence of the specified character in this string.

LastIndexOf (int ch, int fromIndex): searches for the specified index and returns the index of the last occurrence of the specified character in this string.

IndexOf (String str): returns the index of the first occurrence of the specified substring in this String.

IndexOf (String str, int fromIndex): returns the index of the first occurrence of the specified substring from the specified index.

LastIndexOf (String str): returns the index of the rightmost substring in this String.

LastIndexOf (String str, int fromIndex): searches backward from the specified index and returns the last index of the specified substring in this String.

If no index is found in the preceding method,-1 is returned.

5. substring (), concat (), matches (), contains ()

Substring (int beginIndex): returns a new string, which is a substring of this string. The substring starts with a character at the specified index until the end of the string.

Substring (int beginIndex, int endIndex): returns a new string, which is a substring of this string. This substring starts from the specifiedbeginIndexUntil the indexendIndex - 1.

Concat (String str): concatenates a specified String to the end of the String.

Matches (String regex): determines whether the String matches the given regular expression.

Contains (CharSequence s): determines whether the Character Sequence exists in the string.

6. replace (), replaceFirst (), replaceAll ()

Replace (char oldChar, char newChar): returns a new stringnewCharReplace alloldChar. The specific implementation is as follows:

public String replace(char oldChar, char newChar) {        if (oldChar != newChar) {            int len = value.length;            int i = -1;            char[] val = value; /* avoid getfield opcode */            while (++i < len) {                if (val[i] == oldChar) {                    break;                }            }            if (i < len) {                char buf[] = new char[len];                for (int j = 0; j < i; j++) {                    buf[j] = val[j];                }                while (i < len) {                    char c = val[i];                    buf[i] = (c == oldChar) ? newChar : c;                    i++;                }                return new String(buf, true);            }        }        return this;    }

ReplaceFirst (String regex, String replacement): replace the String with the given replacement String to match the first substring of the given regular expression.

ReplaceAll (String regex, String replacement): replace this String with the given replacement String to match each substring of the given regular expression.

7. split (), join ()

String [] split (String regex, int limit): Splits a String based on a regular expression. The number of times that the number control mode applies, thus affecting the length of the result array. If this restrictionNIf the value is greater than 0, the mode will be applied to the maximum number of applications.N-Once, the length of the array will not be greaterNAnd the last entry of the array will contain all input that exceeds the last matching delimiter. IfNIf it is not positive, the pattern will be applied as many times as possible, and the array can be of any length. IfNIf the value is 0, the mode will be applied as many times as possible. The array can have any length, and the trailing null string will be discarded.

String [] split (String regex): splits the String based on the matching of the given regular expression. This method is used to call the Two-Parameter split method by using the given expression and limit parameter 0.

 public String[] split(String regex, int limit) {        /* fastpath if the regex is a         (1)one-char String and this character is not one of the            RegEx's meta characters ".$|()[{^?*+\\", or         (2)two-char String and the first char is the backslash and            the second is not the ascii digit or ascii letter.         */        char ch = 0;        if (((regex.value.length == 1 &&             ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||             (regex.length() == 2 &&              regex.charAt(0) == '\\' &&              (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&              ((ch-'a')|('z'-ch)) < 0 &&              ((ch-'A')|('Z'-ch)) < 0)) &&            (ch < Character.MIN_HIGH_SURROGATE ||             ch > Character.MAX_LOW_SURROGATE))        {            int off = 0;            int next = 0;            boolean limited = limit > 0;            ArrayList<String> list = new ArrayList<>();            while ((next = indexOf(ch, off)) != -1) {                if (!limited || list.size() < limit - 1) {                    list.add(substring(off, next));                    off = next + 1;                } else {    // last one                    //assert (list.size() == limit - 1);                    list.add(substring(off, value.length));                    off = value.length;                    break;                }            }            // If no match was found, return this            if (off == 0)                return new String[]{this};            // Add remaining segment            if (!limited || list.size() < limit)                list.add(substring(off, value.length));            // Construct result            int resultSize = list.size();            if (limit == 0) {                while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {                    resultSize--;                }            }            String[] result = new String[resultSize];            return list.subList(0, resultSize).toArray(result);        }        return Pattern.compile(regex).split(this, limit);    }

String join (CharSequence delimiter, CharSequence... elements ):

String join (CharSequence delimiter, Iterable <? Extends CharSequence> elements): These two methods appear in jdk1.8, similar to String concatenation. However, you can specify the connector delimiter, which is used to connect later elements. The specific implementation is as follows:

Public static String join (CharSequence delimiter, CharSequence... elements) {Objects. requireNonNull (delimiter); Objects. requireNonNull (elements); // Number of elements not likely worth Arrays. stream overhead. stringJoiner joiner = new StringJoiner (delimiter); for (CharSequence cs: elements) {joiner. add (cs); // This method Concatenates the connector} return joiner. toString ();}
8. String case-insensitive conversion:

ToLowerCase (): converts to lowercase

ToUpperCase (): converts to uppercase

9. Other methods:

Trim (): removes the trailing white space of the string.

ToCharArray (): converts a string to a new character array.

ValueOf (): returns the string representation of a parameter. Is a way to convert other types to the String type.

Intern (): When the intern method is called, if the pool already containsStringReturns the string in the pool. OtherwiseStringThe object is added to the pool, and thisStringObject reference.

 

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.