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