替换字符串字符异常



我有一段代码,用破折号替换所有字符串。我该如何使单词包含空格,它不会破折号该空格,而是将其保留为空格。

法典:

public String hiddenWord(){
     word = randomWord.getRandomWord();
     String dashes = word.replaceAll(".", " _ ");
     return dashes;
}

我想要一个空格的例外。现在,连一个空间都破灭了。

尝试

dashes = word.replaceAll("[^ ]", " _ "); // everything but space

dashes = word.replaceAll("\S", " _ "); // non white space

最新更新