---restore content starts---
Always use the C
Cause this kind of intimidating
Now we finally have the courage to learn
Learn the String type first
1 String s1 = "AbCdEf"; 2 String s2 = "abcdef";
char charAt (int index) returns the specified value of the underlying
int CompareTo (string anotherstring) dictionary comparison of two strings
int comparetoignorecase (string str) ignores case comparison of two strings (the result of the following code is 0)
1 String S1 = "AbCdEf"; 2 String s2 = "abcdef"; 3 System.out.println (s1.comparetoignorecase (S2));
Boolean endsWith (string suffix) whether a string ends with another string (the result of the following code is true)
Boolean startsWith (String prefix) (whether to start with a string)
1 String S1 = "AbCdEf"; 2 System.out.println (S1.endswith ("Cdef"));
Boolean equals (Object AnObject) (determines equality)
Boolean equalsignorecase (String anotherstring) (ignoring case to determine equality) the following code result is True
1 String S1 = "AbCdEf"; 2 String s2 = "abcdef"; 3 System.out.println (s1.equalsignorecase (S2));
int indexOf (string str) (fixed string position) the following code results in 3
1 String S1 = "AbCdEf"; 2 System.out.println (S1.indexof ("dEf"));
int lastIndexOf (String str) (the specified string is searched from the right)
int length () (length needless to say)
string substring (int beginindex) string substring (int beginindex, int endIndex) (The following is a new string that contains the first of the following table does not contain a second subscript ·)
1 String S1 = "AbCdEf"; 2 System.out.println (s1.substring (2,5));
Char[] ToCharArray () (converted to an array of type char)
S1.tochararray ()
String toLowerCase () (converts all letters to lowercase letters)
String toString () No use ... But Bufferstring has this operation for converting
String toUpperCase () all changed to uppercase
String trim () remove the preceding space
—————————————————————————— Split Line ——————————————————————————————————————————————
So the operation of the string class is mostly a comparison
In fact, the string class points to a constant, to change the words to use the bufferstring1, stringbuffer the initialization of the object
The initialization of the StringBuffer object is not the same as the initialization of the string class, Java provides special syntax, and typically is initialized with a constructor method.
For example:
StringBuffer s = new StringBuffer ();
This initializes the StringBuffer object as an empty object, which is the error I made.
If you need to create a StringBuffer object with content, you can use:
StringBuffer s = new StringBuffer ("abc");
The content of this initialized StringBuffer object is the string "abc".
It is important to note that StringBuffer and string are of different types and cannot be cast directly, and the following code is wrong:
1StringBuffer s = "abc";//assignment types do not match2StringBuffer s = (stringbuffer) "ABC";//There is no inheritance relationship and cannot be strongly turned3 The code for the StringBuffer between the object and the string object is as follows:4String s ="ABC";5StringBuffer SB1 =NewStringBuffer ("123");6StringBuffer SB2 =NewStringBuffer (s);//convert string to StringBuffer7String S1 = sb1.tostring ();//StringBuffer Convert to String
2, StringBuffer common method splicing Delete change reversal
The methods in the StringBuffer class mainly focus on changes to strings, such as Append, insert, and delete, which are the main differences between the StringBuffer and the string classes.
A, Append method
Public StringBuffer Append (Boolean B)
The function of this method is to append the content to the end of the current StringBuffer object, similar to a string connection. After the method is called, the contents of the StringBuffer object also change, for example:
StringBuffer sb = new StringBuffer ("abc");
Sb.append (TRUE);
The value of the object SB will become "Abctrue".
B, Deletecharat method
Public stringbuffer deletecharat (int index)
The function of this method is to delete the character at the specified position and then form the remaining content into a new string. For example:
StringBuffer sb = new StringBuffer ("Test");
SB. Deletecharat (1);
The function of the code is to delete the string object SB in the index value of 1 characters, that is, to delete the second character, the remaining content constitutes a new string. So the value of the object SB becomes "Tst".
There is also a function-like Delete method:
Public StringBuffer Delete (int start,int end)
The function of this method is to delete all characters within the specified interval, including the start, which does not contain the range of the end index value. For example:
StringBuffer sb = new StringBuffer ("teststring");
SB. Delete (1,4);
The purpose of the code is to remove all characters between the index value 1 (including) to the index value 4 (not included), and the remaining characters to form a new string. The value of the object SB is "Tstring".
C, Insert method
Public stringbuffer Insert (int offset, Boolean b)
The function of this method is to insert content into the StringBuffer object and then form a new string. For example:
StringBuffer sb = new StringBuffer ("teststring");
Sb.insert (4,false);
The purpose of the sample code is to insert a false value at the index value 4 of the object SB, to form a new string, then the value of the object SB after execution is "testfalsestring".
D, Reverse method
Public StringBuffer Reverse ()
The function of this method is to invert the contents of the StringBuffer object and then form a new string. For example:
StringBuffer sb = new StringBuffer ("abc");
Sb.reverse ();
After the reversal, the content in the object SB becomes "CBA".
E, Setcharat method
public void Setcharat (int index, char ch)
The function of this method is to modify the character of the index value in the object to be the new character Ch. For example:
StringBuffer sb = new StringBuffer ("abc");
Sb.setcharat (1, ' D ');
The value of the object SB will become "ADc".
F, TrimToSize method
public void TrimToSize ()
The purpose of this method is to reduce the space waste by narrowing the storage space of the StringBuffer object to the same length as the string length.
In short, in the actual use, string and stringbuffer each have advantages and disadvantages, according to the specific use of the environment, select the corresponding type to use.
StringBuffer s = "abc"; Assignment types do not match
StringBuffer s = (stringbuffer) "ABC"; There is no inheritance relationship and cannot be strongly turned
The code for the StringBuffer between the object and the string object is as follows:
String s = "abc";
StringBuffer sb1 = new StringBuffer ("123");
StringBuffer SB2 = new StringBuffer (s); Convert String to StringBuffer
String S1 = sb1.tostring (); StringBuffer Convert to String
Comparison of string learning notes in Java with char arrays and bufferstring