1. Turning collections into string strings
String s= ""; for (int i = 0; i < numlist.size (); i++) { if (s== "") {s=numlist.get (i); } else {s=s+ "," +numlist.get (i); }}
- Define a list collection, such as:
List<string> numlist=new arraylist<string> (); for (int i=1; i<10; i++) { Numlist.add (String.valueOf (i)); }
- Defines a String s= "" to loop through the values traversed from the numlist.
- Output (character Middle plus ",") such as: s=,1,2,3,4,5,6,7,8,9
- Output results (no spacing between characters) such as: s=123456789
- String.valueof () This method converts a value of non-string type to a numeric value of type string.
2. Convert array to string string
String [] arr = {"abc", "123", "@#&"}; StringBuffer sb = new StringBuffer (); for (int i = 0; i < arr.length; i++) {SB append (Arr[i]);} String ns = sb.tostring (); SYSTEM.OUT.PRINTLN (NS);}
- After running the results are displayed as: [Email protected]#&
- Use String.valueof () This method converts a value of non-string type to a numeric value of type string, and an array of type int is also available. Example: Sb.append (string.valueof (arr[i));
To convert a collection or an array into string strings