The most data we operate on is text, so the content of string text is particularly important. Here is a summary.
The character-related classes are as follows:
1. character. Character is an encapsulation object for char and an operation for a single character.
2. String. String to operate on a string of characters. Immutable class.
1. Definition of string
String is also a type of referenced data, but it is a little different from the general class.
Package test; public class stringdemo {public static void main (string [] ARGs) {string str1 = new string ("ABC"); // The above sentence is equivalent to the following two sentences. Therefore, the above sentence creates two string objects. String str2 = "ABC" is an implicit object creation method, which is unique to string. String str2 = "ABC"; string str3 = new string (str2); system. out. println (str1 = str2); system. out. println (str2 = str3); system. out. println (str1 = str3); system. out. println (str1.equals (str2); system. out. println (str1.equals (str3); system. out. println (str2.equals (str3) ;}} output: falsefalsefalsetruetruetrue
2. String comparison
The equal method of the object is to compare whether the two object reference addresses are equal. In this case, str1.equals (str2) and so on should be true, but the result is true, because string
The equals method in the class to compare the values stored in the two objects. Because the two values are equal, they are true, and = is used to compare the reference addresses of the two objects. This is the usual
A little different.
3. Some common methods of string
Char charat (INT index) // gets the character corresponding to a point
Int compareto (string otherstring) // compare the size in alphabetical order and return positive, negative, or 0, for example, "". compareto ("C") =-2 "C ". compareto ("A") = 2 is actually an ascii code comparison,
From the beginning to the end of the comparison, the current surface has been compared with the size and will not be compared later.
Int indexof (string Str) // index position of the first occurrence of a substring
Int indexof (string STR, int fromindex) // returns the index of the specified character for the first time in this string, starting from the specified index.
Int lastindexof (INT ch) // returns the index of the last occurrence of the specified character in this string.
String substring (INT beginincex, int endindex) // truncates a substring.
More Methods: View API documentation
4. stringbuffer and stringbuilder
3. stringbuffer. Stringbuffer is a thread-safe variable character sequence.
StringBufferThe main operation on isappendAndinsertMethods to accept any type of data. Each method can effectively convert the given data to a string,
Append or insert the character of the string to the string buffer.appendThe method always adds these characters to the end of the buffer.insertThe method adds characters to the specified vertex.
4. sringbuilder. A variable character sequence.
This class providesStringBufferCompatible APIs, but cannot be synchronized. This class is designed for useStringBufferIs used when the string buffer is used by a single thread.
(This is common ). If possible, we recommend that you use this class first, because in most implementations, it is betterStringBufferFast.
Certificate ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Conclusion: when selecting the type of storage string, we need to decide based on the actual situation. If the string is rarely modified but is only viewed, use string. If it is often modified, use stringbuffer.
Or stringbuilder. Further judgment: If the string object used is accessed by multiple threads, use stringbuilder. If the string object is not accessed by multiple threads, use stringbuffer. Because the thread synchronization determines that
To consume resources, the access speed will be slowed down.