简体   繁体   English

如何在Java中删除字符串中的重复字符并修剪字符串中的空白?

[英]How do I remove a repeating characted in my string and trim the white spaces within a string in Java?

I have to test few 10 digit values. 我必须测试几个10位数的值。

          Value: 124324*3213*4324*44*3123**13*
         Expected value: 1243243213432444312313

So far, I have been able to replace the stars using str.replace() function. 到目前为止,我已经能够使用str.replace()函数替换星星。 But I'm unable to remove the white spaces created after removing the star ?. 但是我无法删除在删除星号?之后创建的空白。

尝试这样的事情:

String value = "124324*3213*4324*44*3123**13*".replace("*", "");

Remember that strings are immutable in Java so you have to assign value again: 请记住,字符串在Java中是不可变的,因此您必须再次分配值:

String s = "124324*3213*4324*44*3123**13*";
s = s.replaceAll("\\*", "");

"\\\\*" - is a regular expression, but you don't need to worry about that. “ \\\\ *”-是一个正则表达式,但您不必为此担心。 The important part is that "*" sign has special meaning in regular expressions, so you have to escape it with a backslash. 重要的是,“ *”符号在正则表达式中具有特殊含义,因此您必须使用反斜杠将其转义。 To create a backslash character in Java string you have to use "\\\\" - so the final expression is "\\\\*". 要在Java字符串中创建反斜杠字符,您必须使用“ \\\\”-最终表达式为“ \\\\ *”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM