【发布时间】:2022-01-16 04:25:28
【问题描述】:
我有一个这样的字符列表:[[P, A, H, N, ,], [A, P, L, S, I, I, G, ,], [Y, I, R]]。
将预期输出为字符列表中每个字符的字符串
预期输出:“PAHN,APLSIIG,YIR”。
我的代码
String str = "";
//code#1
for(int j=0; j<list.size(); j++){
str += Arrays.toString(list.get(j).toArray(new Character[0])).replaceAll("[\\]\\[,]",
"").replaceAll(" ", "");
}
代码#1中的str:“PAHNAPLSIIGYIR”
字符串列表
String str = "";
//code#2, converts a list into String
for(int j=0; j<list.size(); j++){
str += Arrays.toString(list.get(j).toArray(new Character[0])).replaceAll(" ", "");
}
代码#2中的str:[P,A,H,N,,][A,P,L,S,I,I,G,,][Y,I,R ]
检查此代码以更好地理解: [https://onecompiler.com/java/3xm55ywr6][1]
我的问题
代码#2中的字符串有两个连续的','字符,可以在字符'N'和'G'之后看到,如果有两个','在一起,如何只替换一个','字符? ??
【问题讨论】:
-
请包含列表列表的定义。
标签: java regex nested-lists