我正在尝试将StringBuilder中的所有字母更改为连字符,并单独保留所有非字母值(例如空格撇号等),即使不列出并比较所有字母,我怎么办呢?
private StringBuilder puzzle = new StringBuilder("I'm the cat")
for (int i = 0; i < guess.length(); i++) {
if (puzzle.charAt(i) == ' '){
puzzle.setCharAt(i, ' '); }
else
puzzle.setCharAt(i, '-');
}
所以输出就像(-'- --- ---)
最佳答案
尝试replaceAll():
System.out.println("I'm the cat".replaceAll("\\w", "-")); //prints: -'- --- ---
关于java - StringBuilder检查字母值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29980175/