-1

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*"
  • 4
    Have you looked at the javadoc of `String`? – Sotirios Delimanolis Mar 25 '14 at 21:52
  • Are third-party libraries fair game? Guava's [`CharMatcher`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/base/CharMatcher.html) lets you define and operate on character classes, e.g. `CharMatcher.anyOf("aeiou").removeFrom(string)` – Louis Wasserman Mar 25 '14 at 21:54
  • Duplicates http://stackoverflow.com/questions/4576352/java-remove-all-occurances-of-char-from-string – chouk Mar 25 '14 at 21:54

2 Answers2

1

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

Óscar López
  • 232,561
  • 37
  • 312
  • 386
0

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

Illidanek
  • 996
  • 1
  • 18
  • 32