Reputation: 1243
What's the best way to remove all \n
, \r
, \t
from a String
in java
?
Is there some sort of library
and method
that can do that for me nicely instead of me having to use string.replaceAll();
multiple times?
Upvotes: 1
Views: 6235
Reputation: 8008
Using regex in java. for future reference, if you want to replace a more complex subset of strings
// strings that you want to remove
String regexp = "str1|str2|str3";
StringBuffer sb = new StringBuffer();
Pattern p = Pattern.compile(regexp);
// here input is your input string
Matcher m = p.matcher(input);
while (m.find())
m.appendReplacement(sb, "");
m.appendTail(sb);
System.out.println(sb.toString());
Upvotes: 2
Reputation: 2105
There is no need to do str.replaceAll
multiple times.
Just use a regex:
str.replaceAll("\\s+", "");
Upvotes: 4