简体   繁体   中英

The filter string - removing some chars

is there a function in Java which removed from a string unwanted chars given by me? If not, what the most effective way to do it. I would like realize it in JAVA

EDIT: But, I want reach for example:

String toRescue="@8*"
String text = "ra@dada882da(*%"
and after call function:
string text2="@88*"

You can use a regular expression, for example:

String text  = "ra@dada882da(*%";
String text2 = text.replaceAll("[^@8*]", "");

After executing the above snippet, text2 will contain the string "@88*" .

The Java String has many methods which can help you, such as

String.replace(char old, char new);
String.split(regex);
String.substring(int beginIndex);

These and many others are described in the javadoc : http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

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