String tmpString = myString.replace( '\'', '*' ); System.out.println( "Original = " + myString ); System.out.println( "Result = " + tmpString );
public static String replaceCharAt(String s, int pos, char c) {
return s.substring(0,pos) + c + s.substring(pos+1);
}
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;
}
public static String removeCharAt(String s, int pos) {
return s.substring(0,pos)+s.substring(pos+1);
}New with JDK1.4, the String class offers the replaceAll() method that can be used with String or char.
String.replaceAll("\n", ""); // Remove all \n
String.replaceAll("\n", "\r"); // Replace \n by \r
Written and compiled by Réal Gagnon ©1998-2007
[ home ]