Remove Numbers From String in Java

You are currently viewing Remove Numbers From String in Java

Here, You are going to know about Remove Numbers From String in Java program. An alphanumeric string is nothing but contains letters, numbers, and special characters.

Now, There are three in-built functions that will help to remove numbers from string using java program. Those are described below:

  1. String.toCharArray(),
  2. String.charAt(),
  3. String.replaceAll().

Let’s see one by one to perform a task in the below example programs.

Remove Numbers From String in Java

The String.toCharArray() function will use to do a task in the below program.

// Java program to remove all the
// digit from string
class CD {

	// Function to remove all the digit
	// from string
	public static String removeAllDigit(String str)
	{
		// Converting the given string
		// into a character array
		char[] charArray = str.toCharArray();
		String result = "";

		// Traverse the character array
		for (int i = 0; i < charArray.length; i++) {

			// Check if the specified character is not digit
			// then add this character into result variable
			if (!Character.isDigit(charArray[i])) {
				result = result + charArray[i];
			}
		}

		// Return result
		return result;
	}

	// Driver Code
	public static void main(String args[])
	{

		// Given alphanumeric string str
		String str = "Codingdiksha2021";

		// Print the modified string
		System.out.println(removeAllDigit(str));
	}
}

Execution

After declaring the class name, We have to remove all strings and initialize a new variable to store the result of the new output string. Later, We will use the String.toCharArray() function to convert the given string into a character array.

After that, We will traverse a character array using a for loop. The if… condition will check if a character does not digit. If it does not digit then add this character into the result variable.

After running this program, The program asks you to enter the input string. After entering the string, We will get the output based on the above completing process.

Finally, We will see the result on the output screen using the system.out.printIn() function.

Output

The output of remove numbers from string in java will be shown below for your reference.

Codingdiksha

Remove Numbers From String in Java

Now, We can use the String.charAt() function to execute a task in the below program.

// Java program to remove all the
// digit from string
class CD {

	// Function to remove all the digit
	// from string
	public static String removeAllDigit(String str)
	{
		String result = "";

		// Traverse the String from start to end
		for (int i = 0; i < str.length(); i++) {

			// Check if the specified character is not digit
			// then add this character into result variable
			if (!Character.isDigit(str.charAt(i))) {
				result = result + str.charAt(i);
			}
		}

		// Return result
		return result;
	}

	// Driver Code
	public static void main(String args[])
	{

		// Given alphanumeric string str
		String str = "Codingdiksha2021";

		// Print the modified string
		System.out.println(removeAllDigit(str));
	}
}

Execution

Both first and second example programs executions are the same. The only difference is the in-built function.

Output

Codingdiksha
Read Also : Java Check If String Contains Special Characters

Remove Numbers From String in Java

Below, We can perform a task using the String.replaceAll() function here.

// Java program to remove all the
// digit from string
class CD {

	// Function to remove all the digit
	// from string
	public static String removeAllDigit(String str)
	{
		// Replaces all the sequence of characters
		// that matches the given regex with
		// the given replacement string
		return str.replaceAll("\\d", "");
	}

	// Driver Code
	public static void main(String args[])
	{

		// Given alphanumeric string str
		String str = "Codingdiksha2021";

		// Print the modified string
		System.out.println(removeAllDigit(str));
	}
}

Explanation

Here, The String.replaceAll() will replace all the sequence of characters that matches the given regex with the given replacement string. Otherwise, The execution process will be the same.

Output

Codingdiksha

Conclusion

I hope this Java tutorial helps you to do “Remove Numbers From String in Java”. If you have any doubts regarding this topic then please let us know via the comment section. We will help you. Share this article with other Java developers.

Manoj S

Hi, I'm Manoj working as a part-time blogger in Coding Diksha. Here, I post about programming to help developers.

Leave a Reply