在Java中寻找快速,简单的方法来更改此字符串
" hello there "
Run Code Online (Sandbox Code Playgroud)
看起来像这样的东西
"hello there"
Run Code Online (Sandbox Code Playgroud)
我用一个空格替换所有这些多个空格,除了我还希望字符串开头的一个或多个空格不见了.
像这样的东西让我部分在那里
String mytext = " hello there ";
mytext = mytext.replaceAll("( )+", " ");
Run Code Online (Sandbox Code Playgroud)
但不完全.
如何使用Java删除字符串中的重复空格(包括制表符,换行符,空格等...)?
如果字符串在单词之间有多个空格:
The cat sat on the mat.
Run Code Online (Sandbox Code Playgroud)
你如何使它成为一个单一的空间?
The cat sat on the mat.
Run Code Online (Sandbox Code Playgroud)
我尝试了这个,但它不起作用:
myText = myText.trim().replace(" ", " ");
Run Code Online (Sandbox Code Playgroud)