1.
To stitch an array into a string
1 public classStringBufferTest2 {2 3 public Static voidmain (string[] Args) {4 //define an array5 int[] arr = {12,21,33,9,2};6 7 //mode 1: Define the function, use string to do stitching8String S1 =arraytostring (arr);9System.out.println ("string concatenation method:" +s1);Ten one //mode 2: define function, use StringBuffer to do stitching aString s2 =arrayToString2 (arr); -System.out.println ("stringbuffer Stitching method:" +s2); - the } - - public StaticString arraytostring (int[] Arr) { - //Define a [string +String s = "["; - //iterating through the array and converting to a string + a for(intx=0;x<arr.length;x++){ at if(x = = Arr.length-1){ -s + = arr[x]+ "]"; - } - Else{ -s + = arr[x]+ ","; - } in } - returns; to } + - public StaticString ArrayToString2 (int[] Arr) { the * //defines a "[" stringbuffer buffer $StringBuffer SB =NewStringBuffer ();Panax NotoginsengSb.append ("["); - the //iterating over the array, and converting to the StringBuffer buffer + for(intx=0;x<arr.length;x++){ a if(x = = Arr.length-1){ the sb.append (arr[x]); + } - Else{ $Sb.append (arr[x]). Append (","); $ } - } -Sb.append ("]"); the //StringBuffer Convert to String - returnsb.tostring (); Wuyi } the}
2.
Reverse the string
1 Importjava.util.Scanner;2 public classStringBufferTest3 {3 4 public Static voidmain (string[] Args) {5Scanner sc =NewScanner (system.in);6System.out.println ("please Enter the data you want to invert");7String str =Sc.nextline ();8 9 //Call Method 1 Reverse1TenString result =reverse1 (str); oneSystem.out.println ("inverted data:" +result); a - //Call Method 2 Reverse2 -String RESULT2 =Reverse2 (str); theSystem.out.println ("inverted data:" +result2); - } - - //mode 1: using string method + public Staticstring Reverse1 (string s) { - + //Define a [string aString S1 = ""; at - //to transfer a string to an array - Char[] ch =S.tochararray (); - - //element traversal of arrays and concatenation of strings - for(intx = ch.length-1;x >= 0;x--){ in - if(x = = 0){ toS1 + =ch[x]; + } - Else{ theS1 + =ch[x]; * } $ }Panax Notoginseng returns1; - } the + //mode 2, using the StringBuffer inversion method a public Staticstring Reverse2 (string s) { the + //converts the input string type to the StringBuffer type - //StringBuffer buffer = new StringBuffer (s); $ $ //to perform the StringBuffer reversal function: - //public StringBuffer Reverse () - //return buffer.reverse (). toString (); the - //Simple VersionWuyi return Newstringbuffer (s). Reverse (). toString (); the } - wu}
3.
Determines whether a string is a symmetric string
Analysis:
There are two ways to do this:
A: Use String to Complete:
first, the string to the array, then the data inside, one by one, the last and the first, inverted two and second ....
B: use StringBuffer to Complete:
Direct inversion to compare the original value
C: Create the method separately, two values that must be confirmed:
A: Return type: Boolean
B: Parameter list: String
1 Importjava.util.Scanner;2 public classStringBufferTest4 {3 4 public Static voidmain (string[] Args) {5 //Create keyboard input6Scanner sc =NewScanner (system.in);7System.out.println ("please Enter the string you want to judge:");8String str =Sc.nextline ();9 Ten //call the first method one BooleanB1 =Symmetry1 (str); aSystem.out.println ("this string is symmetric:" +b1); - - //call the first method of 2 the BooleanB2 =Symmetry2 (str); -System.out.println ("this string is symmetric:" +b2); - - //Call the second method + BooleanB3 =Symmetry2 (str); -System.out.println ("this string is symmetric:" +b3); + } a at //mode 1: Use string method to make a comparison - public Static Booleansymmetry1 (String S) { - //change a string to a group - Char[] C =S.tochararray (); - //to compare, define 2 variables: start and end, respectively, starting from both ends - for(intStart = 0, End = C.length-1;start < end; Start ++,end--){ in if(c[start]! = C[end]) {//to Judge - return false; to + } - } the return true; * } $ Panax Notoginseng //another way of mode 1: - public Static BooleanSymmetry2 (String S) { the //defines a Boolean value + BooleanFlog =true; a the //change a string to a group + Char[] C =S.tochararray (); - $ //to compare, define 2 variables: start and end, respectively, starting from both ends $ for(intStart = 0, End = C.length-1;start < end; Start ++,end--){ - if(c[start]! = C[end]) {//to Judge -Flog =false; the break; - } Wuyi } the returnflog; - } wu - //Mode 2 Using the StringBuffer inversion function about public Static Booleansymmetry3 (String S) { $ return Newstringbuffer (s). Reverse (). toString (). equals (s); - //Change from string to stringbuffer type, then invert it, change back to string type, and let the resulting string be the same as the data in the original comparison - } -}
Several cases of Java 12-5 StringBuffer