1.string. the action method, by default, generates a new string saved in a constant pool without altering the string in the original constant Pool.
Because the constant pool, the string is final and cannot be changed once Created.
Look at the following code:
" ABCDE " ; Str.touppercase (); System. out . println (str); // the output is still abcde, not Abcde. Because the string is final, the action method of changing the contents of a string generates a new string in the constant Pool.
2. Reference variable points to the new Str in the constant pool
" ABCDE " ; = Str.touppercase (); System. Out. println (str);
The constant pool is reborn as a string, and the previous reference variable points to the new address
3. String inversion, using string cache class, StringBuffer or stringbuilder.
Because of the string cache class, a 16-character storage space is pre-created in the heap memory to constantly change the value of string
" ABCDE " ; = new StringBuilder (str); Stringbuilder.reverse (); = stringbuilder.tostring (); System. Out. println (str);//output EDCBA
StringBuilder is to open up a storage space in the heap memory, the modification of StringBuilder is all in modifying StringBuilder object memory Itself.
4. Determine if the string is Symmetric:
" ABCDE " ; = new StringBuilder (str); = stringbuilder.reverse (). toString (); System. Out. println (str==str2);
5. The substring of the output string, such as String str = "@ABC Efg. GHK ";
string[] Strarray = String.Split ("bychar");
String.Split ("bychar") method
Requires output of ABC EFG GHK
" [email protected] ghk EFG " ; = Str.replaceall ("@"""); = Str.split (""); for (String Str1:strarray) { System. out . println (str1); }
5. Write a program that flips the alphabetical order of each word in the following paragraph of text,
"to is or not to is" will become "ot EB ro ton ot eb."
public Static voidmain (string[] Args) {String s="To is or not to IS"; String ss[]= S.split (" "); StringBuffer SB2=NewStringBuffer (); for(inti =0; I < ss.length; i++) {stringbuffer SB=Newstringbuffer (ss[i]); sb.reverse (); sb2.append (sb) ;if(i = = ss.length-1) {sb2.append (".");}Else{sb2.append (" ");}} System. out. println (sb2);}
6.String s= "name=zhangsan age=18 classno=090728";
The above string is split and the result is as Follows:
Zhangsan 18 090728
public Static voidmain (string[] Args) {String s="Name=zhangsan age=18 classno=090728"; string[] SS= S.split (" "); StringBuffer SB=NewStringBuffer (); for(inti =0; I < ss.length; i++) {string[] SS2=ss[i].split ("="); Sb.append (ss2[1]); Sb.append (" "); } System. out. println (sb); }
Summary: the common operation of strings is nothing more than a few
1. String inversion with Stringbuilder.reverse ();
2. String cut string[] strarray = Str.spilt ("spiltchar");
3. string concatenation with Stringbuilder.append ("str");
Java String Common operation questions