Remove non-ASCII characters from String in Java

This tutorial is a guide to (as the name suggests), how to remove all the non-ASCII characters in a string in Java.

ASCII which is an abbreviation of ‘American Standard Code for Information Interchange’, is a method of encoding characters that are based on the order of alphabetic characters in the English language. ASCII integer representations have printable characters, which are any normal characters and non-printable characters, which are characters used to represent keyboard keys, e.g : delete, arrows and enter. Right now the ASCII codes range from 00 which denotes 0(Zero), to 127 which is the Delete character.
Therefore every character that are not present in this Standard Code forms the ‘Non-ASCII’ character group.

Program To remove any non-ASCII characters from a string in Java

import java.util.*;
class nonascii
{
    public static void main()
    {
        Scanner sc=new Scanner(System.in);
        String inp,newstr="";
        char ch;
        int len,i,val;
        System.out.println("Enter your string: ");
        inp = sc.nextLine();
        len=inp.length(); // calculating the length of th string
        for(i=0;i<len;i++)
        {ch = inp.charAt(i);
         if(ch>=00 && ch<=127) //checking each character within the range
         {
             newstr=newstr + inp.charAt(i); // if present,we add it to variable newstr.
            }
        }
        System.out.println(newstr);
    }
}

Output :

Enter your string:
input:This will not be printed ¥
output:This will not be printed

Summary :

To create the code, we first take a string as an input from the user and we find its length. Next, we use a for-loop ranging from the beginning till the last index of the string. Then we extract each character and convert it to its ASCII form using type-casting, and check if the ASCII code lies between the range of 0 and 127. If in case it does we store the character in the variable “newstr”.¬† After the loop exits, we print the value of “newstr” which contains the new string without the Non-ASCII values.

 

The tutorial uses the following functions:

  • .length() : which returns the length of the string
  • .charAt() : which return the character at the given position.

Also learn:

Leave a Reply

Your email address will not be published. Required fields are marked *