I am thinking about using String.replaceAll()
to remove certain characters in my string. It is unclear which characters are going to be removed (i.e. which char
If you're already using the library, Guava makes this easy with CharMatcher
String charsToRemove = "%^#";
String stringToFilter = "I have 20% of my assets in #2 pencils! :^)";
String filtered = CharMatcher.anyOf(charsToRemove).removeFrom(stringToFilter);
I guess, the below code will help you.
String input = "Just to clarify, I will have strings of varying "
+ "lengths. I want to strip characters from it, the exact "
+ "ones to be determined at runtime, and return the "
+ "resulting string.";
String regx = ",.";
char[] ca = regx.toCharArray();
for (char c : ca) {
input = input.replace(""+c, "");
}
System.out.println(input);