Remove a character from a string in Java
Description
The following code shows how to remove a character from a string.
Example
// w w w .j a va2 s. c o m
public class Main {
public static void main(String args[]) {
String str = "this is a test from java2s.com";
System.out.println(removeChar(str,'s'));
}
public static String removeChar(String s, char c) {
String r = "";
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != c)
r += s.charAt(i);
}
return r;
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »