Converts a set or array to a String string.

Source: Internet
Author: User

Converts a set or array to a String string.

1. Convert the set to a String

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 set, for example:
List<String> numList=new ArrayList<String>();for(int i=1; i<10; i++){   numList.add(String.valueOf(i));     }
  • Define String s = "" to cyclically store the value retrieved from numList.
  • Output result (with "," in the middle of the character) such as: s =, 1, 2, 3, 4, 5, 6, 7, 8, 9
  • Output result (no interval between characters) such as: s = 123456789
  • String. valueOf () This method can convert a non-String value to a String value.

 

2. Convert the array to a 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);}
  • The displayed result is abc123 @#&
  • You can use the String. valueOf () method to convert a non-String value to a String value. An int array is also available. Example: sb. append (String. valueOf (arr [I]);

 

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.