在 Java 中将一组字符串转换为逗号分隔的字符串
java 8object oriented programmingprogramming
首先让我们创建一个带有字符串值的集合 −
Set<String>set = new HashSet<>(Arrays.asList("One", "Two", "Three", "Four", "Five", "Six"));
现在,使用 String.join() − 将其转换为逗号分隔的字符串
String str = String.join(", ", set);
示例
以下是在 Java 中将一组字符串转换为逗号分隔的字符串的程序 −
import java.util.*; public class Demo { public static void main(String args[]) { Set<String>set = new HashSet<>(Arrays.asList("One", "Two", "Three", "Four", "Five", "Six")); System.out.println("Set = " + set); String str = String.join(", ", set); System.out.println("Comma separated String: "+ str); } }
输出
Set = [Five, Six, One, Four, Two, Three] Comma separated String: Five, Six, One, Four, Two, Three