To convert a collection or an array into string strings

Source: Internet
Author: User
Tags array to string

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.