Home >Java >javaTutorial >How to remove all specified characters from a string in Java?

How to remove all specified characters from a string in Java?

WBOY
WBOYforward
2023-05-06 19:46:061341browse

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete