我想打印一个从字符集转换而来的字符串。但我想从打印字符串中删除逗号(,)和方括号([])。
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]
答案 0 :(得分:1)
答案 1 :(得分:1)
如果你有一个包含字符的真实Collection,你可以简单地迭代该集合 - 并使用StringBuilder将你想要的所有字符追加到最后的字符串中;像:
StringBuilder validChars = new StringBuilder();
for (Character chr : yourCollection) {
if (chr != ' ' && chr != ',') {
validChars.append(chr);
}
}
首先将所有字符转换为字符串,然后使用replace()创建一个字符较少的新字符串似乎效率低下。