How to delete all occurrences of the specified characters?
The string class does not provide a remove() method, but it provides a replaceAll() method to replace the specified characters with blank characters. You can do it, right?
public class RemoveCharFromString { public static void main(String[] args) { removeCharFromString("沉默王二", '二'); removeCharFromString("chenmowanger", 'n'); } private static void removeCharFromString(String input, char c) { String result = input.replaceAll(String.valueOf(c), ""); System.out.println(result); } }
The output is as follows:
沉默王 chemowager
The above is the detailed content of How to remove all specified characters from a string in Java?. For more information, please follow other related articles on the PHP Chinese website!