[英]remove space before and after - in the string
I am trying to remove extra spaces in the string.To achieve this I used normalizeSpace method in StringUtils class.我正在尝试删除字符串中的多余空格。为此,我在 StringUtils 类中使用了 normalizeSpace 方法。 But the problem is it is not removed the spaces before and after "-"
但问题是它没有删除“-”前后的空格
public static void main(String[] args)
{
String test = " Hi - World Java";
System.out.println(StringUtils.normalizeSpace(test));
}
Output as: "Hi - World Java" The expected output is: "Hi-World Java"输出为:“Hi-World Java” 预期输出为:“Hi-World Java”
Any inputs?有什么输入吗?
Note: Below ticket solution is during concatenating strings.注意:以下票证解决方案是在连接字符串期间。 Where as we have data in a single string.
因为我们在单个字符串中有数据。 So this ticket is not a duplicate ticket.
所以这张票不是重复票。 Remove spaces before a punctuation mark in a string
删除字符串中标点符号前的空格
test = test.replaceAll("[ ]+"," ");
test = test.replaceAll("- ","-");
test = test.replaceAll(" -","-");
test = test.replaceAll("^\\s+","");
The utility removes all extra spaces but leaves one space.该实用程序会删除所有多余的空格,但会留下一个空格。 In other words where it find a sequence of more than one space it removes all but one space.
换句话说,当它找到一个以上空格的序列时,它会删除除一个空格之外的所有空格。 So your result is as expected.
所以你的结果符合预期。 If you need it the way you wrote: "Hi-World Java" then you need your own logic, as specified in some other answers here.
如果您按照您写的方式需要它:“Hi-World Java”,那么您需要自己的逻辑,如此处的其他一些答案中所述。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.