Home > Java > javaTutorial > body text

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

WBOY
Release: 2023-05-06 19:46:06
forward
1331 people have browsed it

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);     } }
Copy after login

The output is as follows:

沉默王  chemowager
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template