Java program to remove all the digits from string || (Different Ways)

Given an alphanumeric string str, the task is to write a Java program that deletes all numbers from the string and prints the modified string.

Examples: 

 
Input: str = “GeeksForGeeks123”

Output: GeeksForGeeks

Explanation: The given string contains digits 1, 2, and 3. We remove all the digit and prints the modified string.



Input: str = “12Java”

Output: Java

Explanation: The given string contains digits 1 and 2. We remove all the digit and prints the modified string.

1. Method 1: Using String.toCharArray() method

  1. Get the String to eliminate all the digit. 
  2. Convert the given string into a person exhibit. 
  3. Instate an unfilled string that stores the outcome. 
  4. Cross the person exhibit from begin to end. 
  5. Check to assume the predetermined person doesn't digit, add this person into the outcome variable. 
  6. Presently, print the outcome.

Below is the implementation of the above approach:

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

	// 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 = "GeeksForGeeks123";

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

Output

GeeksForGeeks
  • Time Complexity: O(N)
  • Space Complexity: O(N)

2. Method 2: Using String.charAt() method

 

  1. Get the String to eliminate all the digits. 
  2. Introduce an unfilled string that stores the outcome. 
  3. Cross the String from beginning to end. 
  4. Check to assume the predefined character isn't digit, add this person into the outcome variable. 
  5. Presently, print the outcome.

Below is the implementation of the above approach:

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

	// 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 = "GeeksForGeeks123";

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

Output

GeeksForGeeks
  • Time Complexity: O(N)
  • Space Complexity: O(1)

3. Method 3: Using String.repla

 

The idea is to use the String.replaceAll() method to replace all character sequences that match the given regular expression with the given replacement string.
 
The following is the implementation of the above method
 
// Java program to remove all the
// digit from string
class GFG {

	// 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 = "GeeksForGeeks123";

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

Output

GeeksForGeeks
  • Time Complexity: O(N)
  • Space Complexity: O(1)

Submit Your Programming Assignment Details