Remove newline, carriage return and tab characters from a string in Java
Description
The following code shows how to remove newline, carriage return and tab characters from a string.
Example
//from w w w .j a va2 s . c om
public class Main {
public static void main(String[] argv) {
System.out.println(removeFormattingCharacters("jav\n\n\n\n\n\t\t\ta2s.com"));
}
public static String removeFormattingCharacters(final String toBeEscaped) {
StringBuffer escapedBuffer = new StringBuffer();
for (int i = 0; i < toBeEscaped.length(); i++) {
if ((toBeEscaped.charAt(i) != '\n')
&& (toBeEscaped.charAt(i) != '\r')
&& (toBeEscaped.charAt(i) != '\t')) {
escapedBuffer.append(toBeEscaped.charAt(i));
}
}
String s = escapedBuffer.toString();
return s;//
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »