How to remove all non-alphanumeric characters from a string in Java

The split() technique for the String class acknowledges a String esteem addressing the delimiter and parts into cluster of tokens (words), treating the string between the event of two delimiters as one token.

For instance in the event that you pass single space ” ” as a delimiter to this technique and attempt to part a String. This strategy considers the word between two spaces as one token and returns a variety of words (between spaces) in the current String.

Assuming the String doesn’t contain the predefined delimiter this strategy returns a cluster containing the entire string as component.

The ordinary articulation “\\W+” matches every one of the not sequential characters (accentuation marks, spaces, highlights and uncommon images) in a string.

  • In this way, to eliminate all non-in order characters from a String −
  • Get the string.
  • Split the acquired string int to a variety of String utilizing the split() strategy for the String class by passing the above indicated customary articulation as a boundary to it.
  • This parts the string at each non-sequential person and returns every one of the tokens as a string cluster.
  • Join every one of the components in the got cluster as a solitary string.

Here is an example Java program that shows how you can eliminate all characters from a Java String other than the alphanumeric characters (i.e., a-Z and 0-9). As may be obvious, this model program makes a String with a wide range of various characters in it, then, at that point, utilizes the replaceAll strategy to strip every one of the characters out of the String other than the examples a-zA-Z0-9. The code basically erases each and every other person it finds in the string, leaving just the alphanumeric characters.

package foo;

public class StringTest {

  public static void main(String[] args) {
      String s = "yo-dude: like, ... []{}this is a string";
      s = s.replaceAll("[^a-zA-Z0-9]", "");
      System.out.println(s);
  }

}

How to replace all non-alphanumeric characters in a string in Python

Replacing all non-alphanumeric characters in a string replaces each character that is not a letter or number with a new character. For example, replacing all non-alphanumeric characters in "a*c!" with 1 results in "a1c1"

Call re.sub(pattern, repl, string) with pattern as "[^0-9a-zA-Z]+" to replace every non-alphanumeric character with repl in string.

a_string = "a*c!"
new_string = re.sub("[^0-9a-zA-Z]+", "1", a_string)

print(new_string)
OUTPUT
a1c1

Given a string str, the undertaking is to eliminate all non-alphanumeric characters from it and print the changed it.

Technique 1: Using ASCII esteems

Since the alphanumeric characters lie in the ASCII esteem scope of [65, 90] for capitalized letter sets, [97, 122] for lowercase letters in order, and [48, 57] for digits. Thus navigate the string character by character and get the ASCII worth of each character. On the off chance that the ASCII esteem isn’t in the over three territories, then, at that point, the person is a non-alphanumeric person. Accordingly skirt such characters and add the rest in another string and print it.

Technique 2: Using String.replaceAll()

Non-alphanumeric characters include every one of the characters aside from letter sets and numbers. It tends to be accentuation characters like interjection mark(!), at symbol(@), commas(, ), question mark(?), colon(:), run(- ) and so on and unique characters like dollar sign($), equivalent symbol(=), in addition to sign(+), apostrophes(‘).

The methodology is to utilize the String.replaceAll technique to supplant every one of the non-alphanumeric characters with an unfilled string.

Below is the implementation of the above approach:

// Java program to remove non-alphanumeric
// characters from a string
class GFG {
 
    // Function to remove non-alphanumeric
    // characters from string
    public static String
      removeNonAlphanumeric(String str)
    {
        // replace the given string
        // with empty string
        // except the pattern "[^a-zA-Z0-9]"
        str = str.replaceAll(
          "[^a-zA-Z0-9]", "");
 
        // return string
        return str;
    }
 
    // Driver Code
    public static void main(String args[])
    {
 
        // Test Case 1:
        String str1 
          = "@!,";
        System.out.println(
          removeNonAlphanumeric(str1));
 
        // Test Case 2:
        String str2 
          = "G{}[]";
        System.out.println(
          removeNonAlphanumeric(str2));
 
        // Test Case 3:
        String str3 
          = "";
        System.out.println(
          removeNonAlphanumeric(str3));
    }
}

Leave a Reply

Your email address will not be published.