Nes*_*ssa 249 java regex string replace
在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)
但不完全.
pol*_*nts 428
试试这个:
String after = before.trim().replaceAll(" +", " ");
Run Code Online (Sandbox Code Playgroud)
String.trim()
trim()
正则表达式也可以只使用一个replaceAll
,但这比trim()
解决方案的可读性低得多.尽管如此,这里提供的只是为了展示正则表达式可以做什么:
String[] tests = {
" x ", // [x]
" 1 2 3 ", // [1 2 3]
"", // []
" ", // []
};
for (String test : tests) {
System.out.format("[%s]%n",
test.replaceAll("^ +| +$|( )+", "$1")
);
}
Run Code Online (Sandbox Code Playgroud)
有3个替代品:
^_+
:字符串开头的任何空格序列
$1
,捕获空字符串_+$
:字符串末尾的任何空格序列
$1
,捕获空字符串(_)+
:任何与上述任何一个都不匹配的空间序列,意味着它位于中间
$1
,捕获单个空间sar*_*son 143
你只需要一个:
replaceAll("\\s{2,}", " ").trim();
Run Code Online (Sandbox Code Playgroud)
在哪里匹配一个或多个空格并用一个空格替换它们然后在开头和结尾修剪空格(实际上可以通过首先修剪然后匹配来反转以使正则表达式更快,就像有人指出的那样).
要快速测试一下,请尝试:
System.out.println(new String(" hello there ").trim().replaceAll("\\s{2,}", " "));
Run Code Online (Sandbox Code Playgroud)
它将返回:
"hello there"
Run Code Online (Sandbox Code Playgroud)
Doc*_*tor 18
这对我很有用: sValue = sValue.trim().replaceAll("\\s+", " ");
xcu*_*pir 17
以下代码将压缩单词之间的所有空格并删除字符串开头和结尾的任何空格
String input = "\n\n\n a string with many spaces, \n"+
" a \t tab and a newline\n\n";
String output = input.trim().replaceAll("\\s+", " ");
System.out.println(output);
Run Code Online (Sandbox Code Playgroud)
这将输出 a string with many spaces, a tab and a newline
请注意,任何不可打印的字符,包括空格、制表符和换行符都将被压缩或删除
有关更多信息,请参阅相应的文档:
k s*_*ath 14
trim() 方法删除前导和尾随空格,并使用 replaceAll("regex", "string to replace") 方法与正则表达式 "\s+" 匹配多个空格并将其替换为单个空格
myText = myText.trim().replaceAll("\\s+"," ");
Run Code Online (Sandbox Code Playgroud)
Git*_*lal 12
"[ ]{2,}"
Run Code Online (Sandbox Code Playgroud)
这将匹配多个空间.
String mytext = " hello there ";
//without trim -> " hello there"
//with trim -> "hello there"
mytext = mytext.trim().replaceAll("[ ]{2,}", " ");
System.out.println(mytext);
Run Code Online (Sandbox Code Playgroud)
OUTPUT:
hello there
Run Code Online (Sandbox Code Playgroud)
小智 9
试试这个吧.
示例代码
String str = " hello there ";
System.out.println(str.replaceAll("( +)"," ").trim());
Run Code Online (Sandbox Code Playgroud)
OUTPUT
hello there
Run Code Online (Sandbox Code Playgroud)
首先,它将用单个空间替换所有空间.比我们必须做修剪String
因为它的开始String
和结束String
将用单个空格替换所有空间如果String
在开始String
和结束时有空格String
我们需要修剪它们.比你得到你想要的String
.
小智 7
String blogName = "how to do in java . com";
String nameWithProperSpacing = blogName.replaceAll("\\\s+", " ");
Run Code Online (Sandbox Code Playgroud)
仅删除前导和尾随空格。
在Java Doc中,“返回一个值为该字符串的字符串,并删除所有前导和尾随空格。”
System.out.println(" D ev Dum my ".trim());
Run Code Online (Sandbox Code Playgroud)
“ Dev Dum my”
替换单词中的所有空字符串,
System.out.println(" D ev Dum my ".replace(" ",""));
System.out.println(" D ev Dum my ".replaceAll(" ",""));
System.out.println(" D ev Dum my ".replaceAll("\\s+",""));
Run Code Online (Sandbox Code Playgroud)
输出:
"DevDummy"
"DevDummy"
"DevDummy"
Run Code Online (Sandbox Code Playgroud)
注意:“ \ s +”是类似于空白字符的正则表达式。
参考:https : //www.codedjava.com/2018/06/replace-all-spaces-in-string-trim.html
到目前为止提供了很多正确的答案,我看到很多赞成。但是,上述方法可以工作,但没有真正优化或不可读。我最近遇到了每个开发人员都会喜欢的解决方案。
String nameWithProperSpacing = StringUtils.normalizeSpace( stringWithLotOfSpaces );
Run Code Online (Sandbox Code Playgroud)
你完成了。这是可读的解决方案。
在 Kotlin 中它看起来像这样
val input = "\n\n\n a string with many spaces, \n"
val cleanedInput = input.trim().replace(Regex("(\\s)+"), " ")
Run Code Online (Sandbox Code Playgroud)