Remove All Unwanted Characters from String in Java

(136 Views)


Remove All Unwanted Characters from String in Java

Strings are widely used in Java programming language, which are a sequence of characters. The Java platform provides the String class to create and manipulate strings. String is mostly used to store text data.

But String can contain various characters like $, &, @,!,(whitespace),* and many more which when used to perform some Business logic operations or if you want to store String into Database can create issues. So, if you want to remove those unwanted characters, here is how you can remove it.

Example: Test.java

public class Test { public static void main(String[] args) { String text = "This $ is @ a sentence"; text = text.replaceAll("[^a-zA-Z0-9]", ""); System.out.println(text); } }

Output

Thisisasentence

Solution Worked 3 UpvotesUpvote

        

Solution Didn't Worked 1 DownvotesDownvote



Comments



Search