I. Common ways to create
String s1= "ABC";
String s2= "ABC";
S1==s2 ==> True
Parse: S1 and S2 point to the same string pool address
Two. How to create infrequently
String s1=New string ("abc");
String s2=new string ("abc");;
S1==s2 ==> false
Parsing: S1 and S2 point to a different string pool address, and new will recreate another memory space to load the string, even if the previous
three . Features of String
(1). The string object is immutable (constant);
(2) Each of the classes that appear to modify the s listening is worth the method, in fact, the creation of a new string object (containing the modified string content);
(3) The efficiency optimization caused by the read-only feature of string is possible;
(4) string literals are stored in a string pool, and string objects take precedence over the word descriptors, avoiding repeated occurrences of strings;
(5) The system is highly efficient for non-modification of string, far more than the other two string classes StringBuffer and StringBuilder ( follow-up explanation )
Four. Common methods for string objects
1. Find string length
public int Long ()//Returns the length of the string
1 String str = new String ("Asdfzxc"), 2 int strlength = Str.length ();//strlength = 7
2. To find a character string in a position
Public char charAt (int index)//Returns the character at the specified position in the string; Note that the first character index in the string is 0, and the last is length ()-1.
1 String str = new String ("Asdfzxc"); 2 char ch = str.charat (4);//ch = Z
3. Extract the Sub-string
You can extract substrings in a string using the substring method of the string class, which has two common parameters:
1) Publicstring substring (int beginindex)//This method takes the remaining characters from the current string to be returned as a new string from the Beginindex position.
2) PublicString substring (int beginindex, int endIndex)//This method starts from the Beginindex position, The character that is removed from the current string to the endIndex-1 position is returned as a new string.
1 String str1 = new String ("Asdfzxc"), 2 string str2 = Str1.substring (2),//str2 = "Dfzxc" 3 string str3 = str1.substring (2,5 );//STR3 = "DFZ"
4. String comparison
1) Publicint CompareTo (string anotherstring)//This method is a dictionary-size comparison of string contents, indicating the size of the current string with the parameter string by returning an integer value. Returns a positive integer if the current object is larger than the argument, and returns a negative integer, equal to 0.
2) Publicint Comparetoignore (String anotherstring)//is similar to the CompareTo method, but ignores case.
3) PublicBoolean equals (Object anotherobject)//compares the current string and argument string, returns True when two strings are equal, otherwise returns false.
4) Publicboolean equalsignorecase (String anotherstring)//is similar to the Equals method but ignores case.
1 String str1 = new String ("abc"), 2 string str2 = new String ("abc"), 3 int a = Str1.compareto (str2),//a>04 int b = str1. CompareTo (STR2);//b=05 boolean c = str1.equals (STR2);//c=false6 Boolean d = str1.equalsignorecase (str2);//d=true
5. String connection
public string concat (String str)//connects the string str in the argument to the back of the current string, and the effect is equivalent to "+".
1 string str = "AA". Concat ("BB"). Concat ("CC"), 2 equivalent to String str = "AA" + "BB" + "CC";
6. Single character lookup in string
1) Publicint indexOf (int ch/string str)///used to find the character or substring in the current string, returns the first occurrence of a character or substring in the current string from the left, or 1 if it does not appear.
2) Publicint indexOf (int ch/string str, int fromIndex)//The method is similar to the first one, except that the method looks backwards from the fromIndex position.
3) Publicint lastIndexOf (int ch/string str)//This method is similar to the first, except that the method looks forward from the end of the string.
4) Publicint lastIndexOf (int ch/string str, int fromIndex)//This method is similar to the second method, except that the method looks forward from the FromIndex position.
1 String str = "I am a good student", 2 int a = Str.indexof (' A '),//a = int b = Str.indexof ("good");//b = "int c = str". IndexOf ("W", 2);//c = -15 int d = str.lastindexof ("a"),//d = 2 ("A", 3), Str.lastindexof
7. Case conversion of characters in string
1) Publicstring toLowerCase ()//Returns a new string after converting all characters in the current string to lowercase
2) Publicstring toUpperCase ()//Returns a new string that converts all characters in the current string to uppercase
1 String str = new String ("AsDF"); 2 string str1 = Str.tolowercase ();//str1 = "AsDF" 3 string str2 = Str.touppercase ();//str 2 = "ASDF"
8. Substitution of characters in a string
1) publicstring replace (char OldChar, char Newchar)//Replaces all OldChar characters in the current string with the character Newchar, and returns a new string.
2) Publicstring Replacefirst (string regex, string replacement)// The method substitutes the contents of the character replacement for the first string encountered in the current string and matches the substring of the Regex match, and the new string should be returned.
3) Publicstring ReplaceAll (string regex, string replacement)// The method replaces any substring encountered in the current string with the string regex matched by the contents of the character replacement, and the new string should be returned.
1 string str = "ASDZXCASD"; 2 string str1 = Str.replace (' A ', ' G ');//str1 = "Gsdzxcgsd" 3 String str2 = str.replace ("ASD", "FGH //STR2 = "FGHZXCFGH" 4 string str3 = Str.replacefirst ("ASD", "FGH");//STR3 = "FGHZXCASD" 5 String STR4 = Str.replaceall ("A SD "," FGH ");//STR4 =" Fghzxcfgh "
9. Other types of methods
1)string trim ()//Truncate the space at both ends of the string, but does not handle the intervening space.
1 string str = "a SD"; 2 string str1 = Str.trim (); 3 int a = Str.length ();//a = int b = str1.length ();//b = 4
2)boolean statwith (string prefix) or boolean endwith (string suffix)// To compare the starting character or substring of the current string prefix and the terminating character or substring suffix is the same as the current string, the overloaded method can also specify the starting position of the comparison offset.
1 String str = "ASDFGH"; 2 Boolean a = Str.statwith ("as"),//a = True3 Boolean b = Str.endwith ("gh");//b = True
3)Regionmatches (boolean b, int firststart, string other, int otherstart, int length)//starting from the Firststart position of the current string, Take a substring of length, and the other string starts at the Otherstart position, specifying another string of length, two string comparisons, and when B is true, the string is not case-sensitive.
4)contains (string str)//Determines whether the parameter S is contained in the string and returns a Boolean type value.
1 String str = "Student"; 2 str.contains ("Stu");//true3 str.contains ("OK");//false
5)string[] Split (string str)//string decomposition of Str as a delimiter, the decomposed character string is returned in an array of strings.
1 String str = "asd!qwe|zxc#"; 2 string[] str1 = Str.split ("!| # ");//str1[0] =" ASD "; str1[1] =" qwe "; str1[2] =" ZXC ";
V. Conversion of strings to basic types
1. String conversion to basic type
The Java.lang package has a byte, short, Integer, Float, double class call method:
1) publicstatic byte Parsebyte (String s)
2) publicstatic short Parseshort (String s)
3) publicstatic short parseint (String s)
4) publicstatic long Parselong (String s)
5) publicstatic float parsefloat (String s)
6) Publicstatic double parsedouble (String s)
For example:
1 int n = integer.parseint ("A"), 2 float f = float.parsefloat ("12.34"), 3 double D = double.parsedouble ("1.124");
2. Binary conversion
Use the methods in the long class to get a variety of binary conversions between integers:
Long.tobinarystring (Long L)
Long.tooctalstring (Long L)
Long.tohexstring (Long L)
Long.tostring (long l, int p)//p as any binary
3. Basic type conversion to string type
The string ValueOf () method is provided in the string class and is used as the base type to convert to a string type.
1)static String valueOf (char data[])
2)static String valueOf (char data[], int offset, int count)
3)static String valueOf (Boolean b)
4)static String valueOf (char c)
5)static String valueOf (int i)
6)static String valueOf (long L)
7)static String valueOf (float f)
8)static String valueOf (double D)
For example:
1 string s1 = string.valueof (2), string S1 = string.valueof (12.34);
3.Java Foundation: Creation and use of String objects