I want to print a string that is converted from a collection of characters. But I want to eliminate the commas (,) and square brackets([]) from the printing string.
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();
I have come to this. It works but prints the string as, say for eg, [a, b, c]
If you have a real Collection containing characters, you can simply iterate that collection - and use a StringBuilder to append all those characters that you want to have in your final string; like:
StringBuilder validChars = new StringBuilder();
for (Character chr : yourCollection) {
if (chr != ' ' && chr != ',') {
validChars.append(chr);
}
}
First turning all characters into a string, then use replace() to create a new string with less characters seems a bit inefficient.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.