Case ONE:/* Requirements: The data in the array is stitched into a string in a specified format * Example: * int[] arr = {x-man}; * Output: * "[1, 2, 3]" * Analysis: * A: Define a String object, except that the content is empty * B: First concatenation of the string "[" * C: Iterate int array, get each element * D: First determine whether the element is the last * is: directly splicing elements and "]" * No: Just stitch the elements and commas and spaces * E: Output The stitched string */public class Stringtest {public static void main (string[] args) {//If the array already exists int[] arr = { 1, 2, 3};//defines a string object, except that the contents are empty string s = "";//The string is preceded by a "[" s + = "[";//Iterate int array, get each element for (int x = 0; x < arr.length; X + +) {//first determine if the element is the last if (x = = arr.length-1) {//directly stitching the element and "]" s + = arr[x];s + = "]";} else {//on stitching elements and commas as well as spaces s + = arr[x];s + = ",";}} Output stitched string System.out.println ("final string is:" + s);}}
The above code is improved by the method:
public class StringTest2 {public static void main (string[] args) {//If an array already exists int[] arr = {1, 2, 3};//write a function that implements the result String result = Arraytostring (arr); System.out.println ("The end result is:" + result);} /* * Two explicit: return value type: string argument list: int[] arr */public static string arraytostring (int[] arr) {//define a string of strings S = "";//string concatenation first A "[" s + = "[";//Iterate int array, get each element for (int x = 0; x < arr.length; X + +) {//first determine if the element is the last if (x = = arr.length-1) {//directly stitching the element and "]" s + = arr[x];s + = "]";} else {//on stitching elements and commas as well as spaces s + = arr[x];s + = ",";}} return s;}}
Case TWO:
* * String Inversion * Example: keyboard input "ABC" * Output: "CBA" * Analysis: * A: Keyboard input a String * B: Define a new String * C: Backward traversal string, get each character * A:length () and Charat () combined with * B: To convert a string into a character array * D: splicing each character with a new string * E: Output new String */public class StringTest3 {public static void main (string[] args) {//keyboard input A string SC Anner sc = new Scanner (system.in); System.out.println ("Please enter a string:"); String line = Sc.nextline ();/*//defines a new string, string result = "";//Convert the string to a character array char[] CHS = Line.tochararray ();//The string is traversed backwards to get every word character for (int x = chs.length-1; x >= 0; x--) {/////////////////with a new string. Result + = Chs[x];} Output a new string System.out.println ("Inverted result is:" + result); *///improvements to function implementation string s = Myreverse (line); SYSTEM.OUT.PRINTLN ("The result of implementing the function is:" + s);} /* Two explicit: return value type: string argument list: string */public static string Myreverse (string s) {//defines a new string, string result = "";//convert string to character number Group char[] CHS = S.tochararray ();//Run backwards through the string to get each character for (int x = chs.length-1; x >= 0; x--) {//////////with new string concatenation of each character result + = CHS[X];} return result;}}
So far the introduction to the use of the string class has been fully introduced, the next entry into the StringBuffer class
A small case of string class such as the first season of getting Started with Java