About string
String s1= "Hello"; String s2= "Hello"; string S3=new string ("Hello"); String S4= "Hello" +s1; String S5= "Hello" +s1; String S6=new string ("Hello" +s1); System.out.println ("value Same, S1 with S2 with memory address, S1==S2:" + (s1==s2)); System.out.println ("There are new,s1 and S3 different memory addresses, S1==S3:" + (s1==S3)); System.out.println ("contains variables, S4 and S5 different memory addresses, S4==S5:" + (s4==S5)); SYSTEM.OUT.PRINTLN ("Variable definition method test:" +S6);
String and memory address
1 /**stirng Common Methods2 * @date 2016-7-33 */4String ss1= "Hello world,my world! ";5 6 CharCh= ' O ';7String ss2= "World";8 intbegin=2;9 intend=10;Ten intLimit=3; One ASystem.out.println ("String length-length:" +ss1.length ()); - -System.out.println ("character/substring in the first position of string-indexof:" +ss1.indexof (CH) + "&" +Ss1.indexof (SS2)); theSystem.out.println ("character/substring in the last position of string-lastindexof:" +ss1.lastindexof (CH) + "&" +Ss1.lastindexof (SS2)); - - //string position starting at 0, interception with begin without end -SYSTEM.OUT.PRINTLN ("substring from int begin to end/to int end position-substring:" +ss1.substring (BEGIN) + "&" +ss1.substring (begin, end)); +System.out.println ("Specify position character-charat:" +Ss1.charat (begin)); -System.out.println ("String SS2-split into string array-split:" +arrays.tostring (Ss1.split (SS2))); + A atSystem.out.println ("Go space before and after-trim:" +Ss1.trim ()); -System.out.println ("Compare content-equal:" +ss1.equals (SS2)); -System.out.println ("Convert to uppercase/lowercase-tolowercase/touppercase:" +ss1.tolowercase () + "&" +ss1.touppercase ()); -System.out.println ("Convert to byte array-getbyte:"); - - byte[] b=ss1.getbytes (); in for(intI=1;i<=ss1.length () -1;i++)//#length-1 is the last one for the subscript. -System.out.print (b[i]+ ""); tostirng Common Methods
1 /**Define modifiable String-stringbuilder2 * @reason frequent manipulation of strings will result in a large number of temporary variables3 * @paramStringBuilder for thread safety4 * @paramStringBuffer, slightly higher performance, generally recommended5 * @date 2016-7-36 */7 8StringBuilder sss1=NewStringBuilder ("Hello"); 9 TenSystem.out.println ("Append to End:" +sss1.append ("World"))); OneSYSTEM.OUT.PRINTLN ("Insert fixed position:" +sss1.insert (4, "hahaha")); ASystem.out.println ("Convert to String:" +sss1.tostring ()); -System.out.println ("Length:" +sss1.length ());Define modifiable String-stringbuilder
Exercises
1 /**determine if the Java file name is correct2 * @Error: Gets the end. Java Method3 1. Last occurrence "." Location of the number4 2, according to the "." Location, get the suffix of the file5 3, judgment "." Location and file suffix name6 7 * @error: The equal statement in the judgment must have parentheses8 * @warning: judgment contains and is not in the first place, Index>0 contains 0 and 12 kinds9 * @date 2016-7-3Ten */ OneString fileName = "Helloworld.java"; A - intindex = Filename.lastindexof ('. ')); -String prefix =filename.substring (index); the - if((Prefix.equals (". Java")) && index>0) -SYSTEM.OUT.PRINTLN ("Java filename is correct");determine if the Java file name is correct
1 /**the letter frequency of the statistic string2 * @date 2016-7-33 */4 5String s = "ALJLKDSFLKJSADJFKLHASDKJLFLKAJDFLWOIUDSAFHAASDASD";6 intnum = 0;7 8 for(intI=0;i<s.length (); i++)9 if(S.charat (i) = = ' A ')Tennum++; One ASystem.out.println ("The number of occurrences of character A:" +num); - the letter frequency of the statistic string
1 /**three-bit comma from back to front2 * @warning StringBuilder converted to a string to output3 * @date 2016-7-34 */5 6 7StringBuilder str=NewStringBuilder ();8Str.append ("JAEWKJLDFXMOPZDM");9 Ten for(intI=str.length () -3;i>0;i-=3) OneStr.insert (i, ', '); A - //converts a StringBuilder object to a string object and outputs - System.out.print (str.tostring ()); theSystem.out.print (str);three-bit comma from back to front
"MOOC" 1.3 Java Common Tool class--string