简体   繁体   中英

Removing characters from string

Say have a string:

String x = "0: 0->1 -2.00";

And I want to remove certain characters, specifically : : > and a few others so that string is now:

0 1 -2.00

Is there a simple way to do this?

To remove just one type of character, you can do this.

myString = myString.replace(":", "");

For more complicated cases, you might need a regular expression, in which case, you'll use replaceAll instead of replace , but you have to be careful about escaping any characters that are "special" in a regular expression.

myString = myString.replaceAll("->|:", "");

As an example, if you wanted to remove -> and : and + , you might write

myString = myString.replaceAll("->|:|\\+", "");

because + is special and needs to be escaped with a backslash.

  x = x.replace(":", "");
  x = x.replace("->", "");

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