One, the String class:
①. Constructing a String Object
Constant object: A string constant object is a sequence of characters enclosed in double quotation marks. For example: "Hello", "12.97", "Boy" and so on.
Characters of a string are encoded using Unicode characters, and one character is two bytes
The string class is more commonly used to construct methods:
string S1 = new String ();
String s2 = new String (string original);
String s3 = new String (char[] a);
String S4 = new String (char[] A,int startindex,int count)
String str = "ABC"; the difference from string str1 = new String ("abc");
Str points to a string constant, str1 points to the heap space address, and heap space points to a string constant
A string is a final class that represents the immutable sequence of characters. The string is immutable. Once a string object is configured, its contents are immutable.
Ii. String Object manipulation
public int Length (): Returns the length of the string
public char charAt (int index): Returns the character of the index position
public boolean equals (Object AnObject): Determines whether strings are equal
public int CompareTo (string anotherstring): Compares the size of two strings, returning the difference of different characters in two strings
public int indexOf (string s): Returns the position of the first occurrence of the string s
public int IndexOf (string s, int startpoint): Returns the position of the first occurrence of the string s from the Stratpoint position
public int lastIndexOf (string s): Returns the position of the last occurrence of the string s
public int LastIndexOf (string s, int startpoint) returns the position of the last occurrence of the string s from the Stratpoint position
public boolean startsWith (string prefix): Returns whether the string starts with prefix
public boolean endsWith (string suffix): Returns whether the string ends with suffix
public boolean regionmatches (int firststart,string other,int otherstart, int length):
Determines whether the current string starts from Firststart with another string other than the other, starting with the Otherstart, length string is equals
public string substring (int startpoint): intercepts substrings starting from startpoint position to the end
public string substring (int start,int end): Intercepts substrings from the startpoint position to the end of an end position
Pubic String replace (char Oldchar,char Newchar):
public string ReplaceAll (String old,string new): Replace old string with new
public string Trim (): Removes all spaces at the beginning and end of the string
public string concat (String str): Connecting two strings
Public string[] Split (string regex): Splits this string according to the match of the given regular expression
③ the conversion of strings to basic data
1. Conversion between string and basic data type, wrapper class
① String---> Basic data type, wrapper class: Parsexxx (String str) that invokes the corresponding wrapper class;
① basic data types, wrapper classes---> Strings: Overloaded valueof () methods that call strings
2. Conversion between a string and a byte array
① string----> byte array: GetBytes () of the calling string
② byte array----> string: Constructor of the calling string new string (byte[] b)
3. Conversion between a string and a character array
① string----> Character array: ToCharArray () that invokes the string;
② character array----> string: Constructor of the calling string new string (char[] c)
Second, StringBuffer class
Java.lang.StringBuffer represents a variable sequence of characters that can be used to delete the contents of a string. Many methods are the same as string, but stingbuffer are variable-length.
StringBuffer is a container.
The ①stringbuffer class has three methods of construction:
1. StringBuffer () A string buffer with an initial capacity of 16
2. StringBuffer (int size) constructs a string buffer of the specified capacity
3. StringBuffer (String str) initializes the content to the specified string content
Common methods of ②stringbuffer class
StringBuffer Append (String s): Add an object at the end of the StringBuffer
StringBuffer Insert (int index, string str): Inserts a string at the specified location str
Public StringBuffer reverse (): Invert StringBuffer
StringBuffer Delete (int startIndex, int endIndex): delete character from StartIndex to EndIndex position
public char charAt (int n): Returns the character of the specified index position n
public void Setcharat (int n, char ch): Sets the character of the specified index position n to Ch
StringBuffer replace (int startIndex, int endIndex, string str): Replaces characters from StartIndex to endIndex positions with string str
public int indexOf (String str): Returns the index of the location where Str is located
Public String substring (int start,int end): Intercepts the characters from start to end
public int Length (): Returns the length of the StringBuffer
Third, StringBuilder class
StringBuilder and StringBuffer are very similar, both represent variable sequences of characters, and the method is the same
String: Immutable character sequence
StringBuffer: Variable character sequences, low efficiency, thread safety
StringBuilder (JDK1.5): variable character sequence, high efficiency, thread insecure
Compare the efficiency of String,stringbuffer,stringbuilder three in addition:
Efficiency from high to the end: Stringbuilde > StringBuffer > String
String text = "";
Long startTime = 0L;
Long endTime = 0L;
StringBuffer buffer = new StringBuffer ("");
StringBuilder builder = new StringBuilder ("");
StartTime = System.currenttimemillis ();
for (int i = 0;i<20000;i++) {
Buffer.append (string.valueof (i));
EndTime = System.currenttimemillis ();
System.out.println ("StringBuffer Execution Time:" + (Endtime-starttime));
StartTime = System.currenttimemillis ();
for (int i = 0;i<20000;i++) {
Builder.append (string.valueof (i));
EndTime = System.currenttimemillis ();
System.out.println ("StringBuilder Execution Time:" + (Endtime-starttime));
StartTime = System.currenttimemillis ();
for (int i = 0;i<20000;i++) {
Text = text + i;}
EndTime = System.currenttimemillis ();
System.out.println ("String Execution Time:" + (Endtime-starttime));
Java string, StringBuffer, StringBuilder