How to convert a generic string to a comma-spaced string, in the simplest way, to a collection of type strings or to an array of type string

Source: Internet
Author: User

Today review code, see some uncle in the generic string type of a collection or array of string type into a comma-spaced strings form when still only focus on the results do not pay attention to the process, "Big uncle" can we take responsibility?

Converts a collection of generic string types to a comma-spaced string form:

Test class:

Package Com.ghj.packageofclient;import Java.util.arraylist;import Java.util.list;import Com.ghj.packageoftool.stringutils;public class Test {public static void main (string[] args) {list<string> Stringlist = new arraylist<string> (); Stringlist.add ("fd422114-0f9f-4f04-bc79-09698121f999"); StringList.add ( "5DE14C97-348C-46B6-8519-F246B5F53D3A"); Stringlist.add ("b2056a9e-8fba-4119-ab5a-79d3ec0100e0"); StringList.add ( "20254564-796e-43d5-bcd2-9fe157f2b18c"); Stringlist.add ("sa31b130-9064-42d9-be2c-321773b2b2b5"); System.out.println (stringutils.stringlisttostring (Stringlist));}}

The first way:

Package Com.ghj.packageoftool;import Java.util.list;public class StringUtils {/** * Converts a collection of generic string types to a comma-spaced string form *  * @author Gao Yingjie */public static String stringlisttostring (list<string> stringlist) {if (stringlist = null | | stringlis T.size () <= 0) {return null;} string result = ""; for (string string:stringlist) {result + = string + ",";} Return result.substring (0, Result.lastindexof (","));}}
Summary: This method is a view of the brain is written out of the people--eldest brother, between String, StringBuffer, and StringBuilderDo you forget the difference?
The second way:

Package Com.ghj.packageoftool;import Java.util.list;public class StringUtils {/** * Converts a collection of generic string types to a comma-spaced string form *  * @author Gao Yingjie */public static String stringlisttostring (list<string> stringlist) {if (stringlist = null | | stringlis T.size () <= 0) {return null;}        StringBuffer StringBuffer = new StringBuffer ();        for (string string:stringlist) {        stringbuffer.append (string + ",");        }        Return stringbuffer.tostring (). substring (0, stringbuffer.tostring (). LastIndexOf (","));}}
Summary: This method is commonly used, but is this the simplest way--look at the third way
The Third Way:

Package Com.ghj.packageoftool;import Java.util.list;public class StringUtils {/** * Converts a collection of generic string types to a comma-spaced string form *  * @author Gao Yingjie */public static String stringlisttostring (list<string> stringlist) {if (stringlist = null | | stringlis T.size () <= 0) {return null;} Return stringlist.tostring (). ReplaceAll ("^\\[| |\\]$", "");}}
Summary: Brother Ah, this is the simplest way AH!!!
Converts an array of type string to a comma-spaced string form:

Test class:

Package Com.ghj.packageofclient;import Com.ghj.packageoftool.stringutils;public class Test {public static void main ( String[] (args) {String [] Stringarray = new string[]{"fd422114-0f9f-4f04-bc79-09698121f999", " 5DE14C97-348C-46B6-8519-F246B5F53D3A "," b2056a9e-8fba-4119-ab5a-79d3ec0100e0 "," 20254564-796e-43d5-bcd2-9fe157f2b18c "," sa31b130-9064-42d9-be2c-321773b2b2b5 "}; System.out.println (stringutils.stringarraytostring (Stringarray));}}
The simplest way to convert a set of generic string types to a comma-spaced string is given here, which is the simplest way to convert an array of type string into a comma-spaced string:

Package Com.ghj.packageoftool;import Java.util.arrays;public class StringUtils {/** * Converts an array of type string into a comma-spaced string form *  * @author Gao Yingjie */public static string Stringarraytostring (string [] stringarray) {if (Stringarray = = NULL | | stringarray.lengt H <= 0) {return null;} Return arrays.tostring (Stringarray). ReplaceAll ("^\\[| |\\]$", "");}}

How to convert a generic string to a comma-spaced string, in the simplest way, to a collection of type strings or to an array of type string

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.