String
Concept
String S1 = "abc"; S1 is a class-type variable, and "ABC" is an object
The maximum character of a string, once initialized, cannot be changed, and re-assignment refers to the new object
There is an object in memory
String s2 = new String ("abc"); There are two of objects in memory
S1 = = S2//false//The address is judged
S1.equals (S2)//true//determines the value//string redefined. Equals () method
(Equals () where the address is judged elsewhere)
String s3 = "ABC";
S1 = = S3; True//Because the s1= "ABC" constant has been defined earlier, so S3 points to the "ABC" defined by S1 to save space
Method
Get
int length ()//Get length, return int
char charAt (int index)//return position on a character based on position
int indexOf (int ch)//Return CH (ASCII code) first occurrence of CH position, if none return-1
int indexOf (int ch, int fromIndex)//starting at the specified position of the FromIndex, the position of CH appears for the first time
int indexOf (string str)//Returns the position of the first occurrence of STR in a string
int indexOf (String str, int fromIndex)//position at the beginning of the first occurrence of the specified position
int lastIndexOf (int ch)//Return CH where the last occurrence in the string
Judge
Boolean contains (str)//Determine if a substring is contained in a string
int indexOf (str)//Returns the first occurrence of STR, if no return-1
Boolean isEmpty ()//determines whether the character length is 0
Boolean startsWith (str)//Determine if string starts with Str
Boolean endwith (str)//Determine if the string ends with Str
Boolean equals (str)//determines if the contents of the string are identical, and the Equals method in the object class is replicated
Boolean equalsignorecase ()//Determine if content is the same, ignoring case
Comparing strings in a natural order
int CompareTo (string); Same return 0, different returns the difference of the first different value (ASCII code)
Transformation
static string ValueOf (char[])//Convert character array to string
static string valueOf (int)//Converts the base type to a string, equivalent to "";
String toUpperCase (); Convert string to uppercase
String toLowerCase (); String converted to lowercase
Byte[] GetBytes ()//convert string to byte array
Replace:
String Replace (Oldchar,newchar)//return new string
Cutting
String[] Split (regex); Splits a string into a regex as a split point, returns a string array substring, and gets a portion of the string
String substring (begin); From the specified position to the end, if the corner label does not exist, a string will appear out of bounds
String substring (begin,end); Contains header, does not contain tail
Remove both spaces
String trim ();
String Buffer StringBuffer
StringBuilder appears after JDK1.5
StringBuffer//thread synchronization, High security
StringBuilder//thread out of sync, high efficiency, single thread recommended
Characteristics
The length is variable.
Will eventually become a string through the ToString method
Multiple data types can be manipulated directly
StringBuffer sb = new StringBuffer (); Defining objects
StringBuilder sb = new StringBuilder (); Defining objects
Method
Increase
Sb.append (ABC); Add data to the end of an existing data
Sb.insert (1, "QQ"); Inserting data into a specified position (first parameter)
StringBuffer sb = new StringBuffer ("aaaaaaa");
Sb.insert (3, BBB); Change a string to AAAABBBAAA
Sb.append (x) = = Sb.insert (Sb.length (), X)
Delete
Sb.delete (start, end)//delete data in buffer, contains start, does not contain end
Sb.deletecharat (index); Delete the character at the specified position
Get
char charAt (int index)//get the specified position character
int indexOf (string str)//Returns the position of the first occurrence of the specified string
int lastIndexOf (string str)//Returns the position of the last occurrence of the specified string
int length (); return string length
string substring (int start, int end)//returns the starting angle to the end of the index
Modify
Sb.replace (start,end,string); Replace the value between the start position and the end position with a string
Sb.setcharat (int index,char ch); Replace the character of the index position with CH
Reverse
Sb.reverse ();
Stores the specified data in the buffer into the specified array
Sb.getchars (1,3,char[] chs,4); Stores the data in the buffer with the corners labeled 1 and 2 in the array CHS, starting with the corner Mark 4 in the array.
Output
System.out.println (sb.tostring); The output needs to be converted to string type first
StringBuilder SB//A variable sequence of characters.
Method
Increase
StringBuilder append (Object obj)//Appends the string representation of the object parameter.
StringBuilder Insert (int offset, object obj) inserts the string representation of the object parameter into this sequence of characters.
Delete
StringBuilder deletecharat (int index)//Removes the char at the specified position of this sequence.
Modify
void ensurecapacity (int minimumcapacity)//ensures that the capacity is at least equal to the specified minimum value.
StringBuilder reverse ()//Invert
void Setcharat (int index, char ch)//sets the character at the given index to Ch.
Inquire
int capacity ()//returns the current capacity.
char charAt (int index)//returns the char value at the specified index in this sequence.
StringBuilder Delete (int start, int end)//removes the characters in the substring of this sequence.
int indexOf (string str)//Returns the index of the specified substring that appears for the first time in the string.
int length ()//(number of characters).
string substring (int start)//Returns a new string containing the subsequence of the characters currently contained in this sequence of characters.
Java string (Common)