简体   繁体   中英

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.

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

So far, I have been able to replace the stars using str.replace() function. 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:

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 "\\\\*".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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