Interview Kickstart has enabled over 21000 engineers to uplevel.
Given: A string containing some ASCII characters.
Task: To remove all non-alphanumeric characters in the given string and print the modified string.
In this article, we’ll explore:
Alpha stands for alphabets, and numeric stands for a number. So, alphabets and numbers are alphanumeric characters, and the rest are non-alphanumeric.
Examples of alphanumeric characters: ‘a’, ‘A’, ‘p’, ‘2’
Examples of non-alphanumeric characters: ‘!’, ‘{‘, ‘&’
Input: Interview!@Kickstart23
Output: InterviewKickstart23
Here the symbols ‘!’ and ‘@’ are non-alphanumeric, so we removed them.
Input: Interview_{@Kick}start
Output: InterviewKickstart
Here the symbols ‘_’, ‘{’, ‘}’ and ‘@’ are non-alphanumeric, so we removed them.
Input: InterviewKickstart23
Output: InterviewKickstart23
Here, there’s no need to remove any characters because all of them are alphanumeric.
To remove non-alphanumeric characters in a given string in Java, we have three methods; let’s see them one by one.
If we see the ASCII table, characters from ‘a’ to ‘z’ lie in the range 65 to 90. Characters from ‘A’ to ‘Z’ lie in the range 97 to 122, and digits from ‘0’ to ‘9’ lie in the range 48 to 57.
Thus, we can differentiate between alphanumeric and non-alphanumeric characters by their ASCII values.
In this method, we’ll make an empty string object, traverse our input string and fetch the ASCII value of each character. If the ASCII value is in the above ranges, we append that character to our empty string. Else, we move to the next character.
After iterating over the string, we update our string to the new string we created earlier.
Code:
/*
Java program to remove non-alphanumeric characters with
Method 1: Using ASCII values
*/
public class Main {
// Function to remove the non-alphanumeric characters and print the resultant string
public static String rmvNonalphnum(String s)
{
String temp = "";
for(int i=0;i<s.length();i++)
{
// current character
char c = s.charAt(i);
// get the ascii value of current character
int ascii = (int)c;
// check if the ascii value in our ranges of alphanumeric and if yes then print the character
if((ascii>=65 && ascii<=90) || (ascii>=97 && ascii<=122) || (ascii>=48 && ascii<=57))
{
temp+=c;
}
}
return temp;
}
// Driver Code
public static void main(String args[])
{
// Test Case 1:
String s1 = "Interview!@Kickstart23";
// calling the function
s1 = rmvNonalphnum(s1);
System.out.println(s1);
// Test Case 2:
String s2 = "Interview_{@Kick}start";
// calling the function
s2 = rmvNonalphnum(s2);
System.out.println(s2);
// Test Case 3:
String s3 = "InterviewKickstart23";
// calling the function
s3 = rmvNonalphnum(s3);
System.out.println(s3);
}
}
In this approach, we use the replace() method in the Java String class. We use this method to replace all occurrences of a particular character with some new character.
About String.replace()
public String replace(char a, char b)
a: old character that we need to replace.
b: new character which needs to replace the old character.
Resultant string after replacements.
In this approach, we loop over the string and find whether the current character is non-alphanumeric or not using its ASCII value (as we have already done in the last method). If it is non-alphanumeric, we replace all its occurrences with empty characters using the String.replace() method.
Code:
/*
Java program to remove non-alphanumeric characters with
Method 2: Using String.replace()
*/
public class Main {
// Function to remove the non-alphanumeric characters and print the resultant string
public static String rmvNonalphnum(String s)
{
for(int i=0;i<s.length();i++)
{
// current character
char c = s.charAt(i);
// get the ascii value of current character
int ascii = (int)c;
// check if the current character is non-alphanumeric if yes then replace it's all occurrences with empty char ('\0')
if(!((ascii>=65 && ascii<=90) || (ascii>=97 && ascii<=122) || (ascii>=48 && ascii<=57)))
{
s = s.replace(c,'\0');
}
}
// returning the resultant string
return s;
}
// Driver Code
public static void main(String args[])
{
// Test Case 1:
String s1 = "Interview!@Kickstart23";
// calling the function
s1 = rmvNonalphnum(s1);
System.out.println(s1);
// Test Case 1:
String s2 = "Interview_{@Kick}start";
// calling the function
s2 = rmvNonalphnum(s2);
System.out.println(s2);
// Test Case 1:
String s3 = "InterviewKickstart23";
// calling the function
s3 = rmvNonalphnum(s3);
System.out.println(s3);
}
}
In this approach, we use the replaceAll() method in the Java String class. This method returns the string after replacing each substring that matches a given regular expression with a given replace string.
About String.replaceAll()
public String replaceAll(String rgx, String replaceStr)
replaceStr: the string which would replace the found expression.
rgx: the regular expression that this string needs to match.
Resultant string after replacements.
So, we use this method to replace the non-alphanumeric characters with an empty string.
Code:
/*
Java program to remove non-alphanumeric characters with
Method 2: Using String.replace()
*/
public class Main {
// Function to remove the non-alphanumeric characters and print the resultant string
public static void rmvNonalphnum(String s)
{
// replacing all substring patterns of non-alphanumeric characters with empty string
s = s.replaceAll("[^a-zA-Z0-9]", "");
}
// Driver Code
public static void main(String args[])
{
// Test Case 1:
String s1 = "Interview!@Kickstart23";
// calling the function
rmvNonalphnum(s1);
System.out.println(s1);
// Test Case 1:
String s2 = "Interview_{@Kick}start";
// calling the function
rmvNonalphnum(s2);
System.out.println(s2);
// Test Case 1:
String s3 = "InterviewKickstart23";
// calling the function
rmvNonalphnum(s3);
System.out.println(s3);
}
}
Whether you’re a Coding Engineer gunning for Software Developer or Software Engineer roles, or you’re targeting management positions at top companies, IK offers courses specifically designed for your needs to help you with your technical interview preparation!
If you’re looking for guidance and help with getting started, sign up for our free webinar. As pioneers in the field of technical interview prep, we have trained thousands of Software Engineers to crack the most challenging coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!
————
Article contributed by Omkar Deshmukh
Attend our webinar on
"How to nail your next tech interview" and learn