Summary About the String class

String class: can not be modified (with a final modification)

(1) Constructor

public String ()
 public String ( byte [] bytes): the byte to a string array to a
 public String ( byte [] bytes, int index, int length): a part of the byte array translated into strings
 public String ( char [] value, int index, int COUNT): a part of the array of characters is translated into strings
 public string (string Original): a string object initialization newly created, it represents the same sequence of characters parameter; in other words, the newly created string is a copy of the parameter string.

(2) The method of Example

  1) determining function

public  Boolean the equals (Object anObject) determines whether the character string as
 public  Boolean equalsIgnoreCase (String anotherString) determines whether the same string, ignoring case
 public  Boolean the contains (CharSequence S) which determines whether a string contains the string, CharSequence is an interface, String class implements the interface, so the types of parameters can be passed Sting
 public  Boolean whether startsWith (string prefix) to nothing at the beginning of the string is determined
 public  Boolean endsWith (string suffix) determines whether the end of the string in what
 public  Boolean isEmpty () determines string is empty string

  2) acquisition function

int length (): Get the length of the string
 char the charAt ( int index): Gets a character specified index
 int the indexOf ( int CH): Get the specified character appears in the index of the first string
 int the indexOf (String STR) : returns the string index within this string of the first occurrence of
 int indexOf ( int CH, int fromIndex): returns the character index of the first occurrence of the specified position in this string
 int indexOf (string str , int returns the index of the string in the string after the first occurrence of the specified location: fromIndex)
 int position lastIndexOf () appears in the last
The substring String ( int Start): Start from the specified location string taken, not by default to the end
The substring String ( int Start, int End): starting at the specified position to a specified position of the end of characters, taken FIG.

  3) conversion function

byte [] the getBytes (): the string into a byte array
 char [] toCharArray (): converts a string to a character array
 static String valueOf ( char [] CHS): the array of characters translated into strings
 static String valueOf ( int I): the int type data into a string
Note: valueOf String class method can be any type of data into a string
String toLowerCase (): string to lower case
String toUpperCase (): turn a string to uppercase
String concat (String str): The string concatenation, and only string concatenation. + When spliced string may be any type

  4) Other

public String Replace ( char occurrences of oldChar in, char newChar) // replacement character 
public String Replace (CharSequence target, CharSequence Replacement) // replacement string 
String TRIM (); // longitudinal removing spaces 
public  int the compareTo (String anotherString) // comparison, were compared according to the ASCII code table 
public  int compareToIgnoreCase (String str) // Compare

About compareTo () method: character by character as the difference when the difference is zero when compared to compare the next character to know the end.

String s1 = "abc";
String s2 = "ABC";
int b = s1.compareTo(s2);
System.out.println(b); //32 ==> 'a'-'A' ==> 97-65=32
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;
            }
            k++;
        }
        return len1 - len2;
    }

StringBuffer class: You can modify, thread-safe, the parent class is AbstractStringBuilder.

Internal StringBuffer class has a character buffer array, the array is actually operated. And String class also has a character array, but the array with a final modification, and initialized in the constructor, it can not be modified.

(1) Constructor

public the StringBuffer (): null constructor parameters, a default capacity is 16
 public the StringBuffer ( int Capacity): specified capacity
 public the StringBuffer (String STR): the given initial character string array

(2) a method of adding

public synchronized StringBuffer append
public synchronized StringBuffer insert

(3) Delete method

deleteCharAt ( int index) Delete character at the specified index
the Delete ( int Start, int End): delete the beginning to the end position of the character

(4) inversion method

public synchronized StringBuffer reverse()

(5) intercepting function, the return value is String

public String the substring ( int Start): intercepted from the specified position to the end of
 public String the substring ( int Start, int End): taken at the specified position to the end position, comprising a start position, end position is not included

(6) String and StringBuffer conversion

String transfer StringBuffer:
 . 1) constructor new new StringBuffer (String STR)
 2 the append method) using the StringBuffer

StringBuffer转String:
1) String class constructor new new String (StringBuffer S)
 2 ) The method of StringBuffer toString
 substring methods. 3) of StringBuffer

Guess you like

Origin www.cnblogs.com/liualex1109/p/11828621.html