Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我无法在连字符前后删除一个空格。我试过了: -
sample.replaceAll("[\\s\\-\\s]","")
和排列无济于事。我不想剥离所有空间,也不想剥离所有中间空间。我试图解析一个基于" "但想要消除的字符串"-"。任何见解表示赞赏。
" "
"-"
[\s\-\s]是一个字符类,不匹配space followed by - followed by space. 它匹配任何字符 - space、 和-,并将它们替换为empty string。
[\s\-\s]
space followed by - followed by space
space
-
empty string
你可以使用这个: -
sample = sample.replaceAll("[ ]-[ ]","-");
或者,甚至String.replace可以在这里工作。你真的不需要replaceAll: -
String.replace
replaceAll
sample = sample.replace(" - ", "-");