将 char 集合转换为不带逗号和方括号的字符串

convert a collection of char into string without the comma and square brackets

我想打印一个由字符集合转换而来的字符串。但我想从打印字符串中删除逗号 (,) 和方括号 ([])。

List<Character> word_to_show = new ArrayList<Character>(); 
for(char ch:jumbled_word.toCharArray()){ 
 word_to_show.add(ch); 
} 
Collections.shuffle(word_to_show); 
for (Object ch: word_to_show) 
  System.out.print((Character) ch ); 
System.out.println(); 
send_to_timer = word_to_show.toString(); 

我已经到了。它可以工作,但会将字符串打印为例如 [a, b, c]

您可以使用replace()

string.replace(",","").replace("[","").replace("]","")

Demo

如果您有一个包含字符的真实集合,您可以简单地迭代该集合 - 并使用 StringBuilder 附加所有您想要在最终字符串中包含的字符;喜欢:

StringBuilder validChars = new StringBuilder();
for (Character chr : yourCollection) {
  if (chr != ' ' && chr != ',') {
     validChars.append(chr);
  }
}

先把所有字符转成字符串,再用replace()创建一个字符少的新字符串,效率有点低