将 String 转换为 Java 中的逗号分隔列表
首先,设置一个字符串值列表−
List<String> myList = new ArrayList<>( Arrays.asList("One", "Two", "Three", "Four"));
现在,使用 String.join() 将它们设置为逗号分隔列表−
String str = String.join(", ", myList);
示例
以下是将字符串转换为 Java 中逗号分隔列表的程序−
import java.util.*; public class Demo { public static void main(String args[]) { List<String> myList = new ArrayList<>(Arrays.asList("One", "Two", "Three", "Four")); System.out.println("List = " + myList); // comma separated String str = String.join(", ", myList); System.out.println("String (Comma Separated) = " + str); } }
输出
List = [One, Two, Three, Four] Comma separated String: One, Two, Three, Four
广告