Let's talk about how to convert a String-type set or String-type array to a comma-separated string in the simplest way.
Today's review Code shows that some grandfathers still ignore the process when converting a String-type set or String-type array into a comma-separated String, can I take responsibility for "grandpa?
Convert a String-type set to a comma-separated String:
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));}}
Method 1:
Package com. ghj. packageoftool; import java. util. list; public class StringUtils {/*** converts a String-type set to a comma-separated String ** @ author Gao huanjie */public static String stringListToString (List <String> stringList) {if (stringList = null | stringList. size () <= 0) {return null;} String result = ""; for (String string: stringList) {result + = String + "," ;}return result. substring (0, result. lastIndexOf (","));}}
Summary: This method is written by the mentally handicapped person, big brother,
Between String, StringBuffer, and StringBuilderDo you forget the difference?
Method 2:
Package com. ghj. packageoftool; import java. util. list; public class StringUtils {/*** converts a String-type set to a comma-separated String ** @ author Gao huanjie */public static String stringListToString (List <String> stringList) {if (stringList = null | stringList. 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 method? -- look at the third method.
Method 3:
Package com. ghj. packageoftool; import java. util. list; public class StringUtils {/*** converts a String-type set to a comma-separated String ** @ author Gao huanjie */public static String stringListToString (List <String> stringList) {if (stringList = null | stringList. size () <= 0) {return null;} return stringList. toString (). replaceAll ("^ \ [| \] $ ","");}}
Conclusion: Brother, this is the simplest way !!!
Convert String-type arrays into comma-separated strings:
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 preceding describes the simplest way to convert a set of generic String types into comma-separated strings, therefore, the simplest method to convert an array of the String type into a comma-separated String is provided:
Package com. ghj. packageoftool; import java. util. arrays; public class StringUtils {/*** converts String-type Arrays into comma-separated strings ** @ author Gao huanjie */public static String stringArrayToString (String [] stringArray) {if (stringArray = null | stringArray. length <= 0) {return null;} return Arrays. toString (stringArray ). replaceAll ("^ \ [| \] $ ","");}}