Characteristics of a String object
-
The data of the string object cannot be changed!
-
String type object encapsulates an array of strings
-
No action can change the contents of this character array
string s = "123"; string ss = s;s = s+ "ABC"; System.out.println (s);//123abcsystem.out.println (ss);//123
Description: A string reference variable was changed in the above code, but the string did not change! The advantage is that strings can be used "as" Basic types!
Principle:
The reuse of string constants
java are reusable in every possible use! The benefit is saving resources (memory)
-
String literal (direct amount) reuses the same string object at the same time as the content. String S1 = "123ABC"; String s2 = "123ABC"; S1 S2 is a reference variable of string type//"123ABC" is a direct volume (literal)
-
String constant is also involved in reuse!
-
Literal, constant operation result is a string, also reuse the same string s4 = "123ABC"; String S5 = 123 + "abc",//1+ "23abc" System.out.println (S4==S5),//true
-
String variable, the result of the operation of the variable and the newly created string object are not involved in reuse!! String name = In.nextline ();//tom string S1 = "Tom and Jerry"; String s2 = name + "and Jerry"; System.out.println (S1==S2);//false string s3 = new String ("Tom and Jerry"); System.out.println (S3==S1);//false
Principle:
Classic topics:
String S1 = "1" + "+" + "abc"; String s2 = "1" +23+ "abc"; String s3 = ' 1 ' +23+ "abc"; System.out.print (S1==S2); System.out.print (S1==S3); execution result of the above code: A.truetrue b.truefalse c.falsetrue D.falsefalse
Characters in a string
A character array is encapsulated in the string, and the characters in the string are data of type char.
The char type is an integer and is a Unicode encoding of a character.
16-bit unsigned integer that occupies 2 bytes
Case:
String s = "Tom and Jerry";//0123456789012char C = S.charat (4); System.out.println (c);//asystem.out.println ((int.) c);//97
IndexOf method
Find the position of a character in a string:
IndexOf () 1. If there are duplicates, find the first position on the left 2. If not found, return-1
Case:
String s = "Tom and Jerry"; int i = S.indexof (' a '); System.out.println (i);//4i = S.indexof (' R '); System.out.println (i);//10i = S.indexof (' X '); System.out.println (i);//-1
Str.indexof ("Find string", starting position)
String url = "http://tedu.cn/index.html"; int i = Url.indexof ("/", 7); System.out.println (i);
LastIndexOf
Reverse-Order Lookup: Right-to-left lookup, returning the position of a character
String url = "http://tedu.cn/index.html"; int i = Url.lastindexof ("/");//14system.out.println (i);//14
Find Package Java.lang
Found class String
Find Method LastIndexOf ()
Substring method
truncate a portion of the string as a substring
Url.substring (start position)//from start position to last intercept as substring string url = "Http://tedu.cn/index.html"; string filename = url.substring;//filename = index.htmlurl.substring (start position, end position)//from start position to end position intercept as substring string url = " Http://tedu.cn/index.html ";//01234567890123456//includes the starting not including the end position of string str = url.substring (7, 14); String str = url.substring (7, 7+8);
Trim
String str = "\ t Tom \ r \ n"; String s = Str.trim ();
StartsWith EndsWith
Detects whether a string begins or ends with a specified string
String str = "Hello world!"; Boolean B = Str.startswith ("Hello"),//trueb = Str.startswith ("World"),//falseb = Str.endswith ("World");//falseb = Str.endswith ("!"); /true
Case:
String name = "Demo." JPG ";" if (Name.tolowercase (). EndsWith (". jpg")) {System.out.println ("picture file");}
StringBuilder
Java provides an API for calculating strings, which performs well:
Case:
String s = "A"; s = s + "1"; s = s + "1"; s = s + "1"; System.out.println (s);
Principle:
performance comparison:
String s = "A"; long t1 = System.currenttimemillis (); for (int i=0; i<10000; i++) { s = s+ "1"; } long t2 = system.currenttimemillis (); system.out.println (S.length ()); system.out.println (T2-T1); stringbuilder ss = new stringbuilder ("A"); t1= System.nanotime (); for (int i=0; i<10000; i++) { ss.append ("1"); } t2 = System.nanotime (); system.out.println (Ss.length ()); System.out.println (T2-T1);
A String object in Java