Given alphanumeric string str, the task is to write a Java program to remove all the digit from this 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.
Method 1: Using String.toCharArray() method
- Get the String to remove all the digit.
- Convert the given string into a character array.
- Initialize an empty string that stores the result.
- Traverse the character array from start to end.
- Check if the specified character is not digit then add this character into the result variable.
- Now, print the result.
Below is the implementation of the above approach:
Java
// 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)); } } |
GeeksForGeeks
- Time Complexity: O(N)
- Space Complexity: O(N)
Method 2: Using String.charAt() method
- Get the String to remove all the digit.
- Initialize an empty string that stores the result.
- Traverse the String from start to end.
- Check if the specified character is not digit then add this character into the result variable.
- Now, print the result.
Below is the implementation of the above approach:
Java
// 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)); } } |
GeeksForGeeks
- Time Complexity: O(N)
- Space Complexity: O(1)
Method 3: Using String.replaceAll() method
The idea is to use String.replaceAll() method that replaces all the sequence of characters that matches the given Regular Expression with the given replacement string.
Below is the implementation of the above approach:
Java
// 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)); } } |
GeeksForGeeks
- Time Complexity: O(N)
- Space Complexity: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimum digits to be removed to make either all digits or alternating digits same
- Different ways for Integer to String Conversions In Java
- Sort a String in Java (2 different ways)
- Different ways for String to Integer Conversions in Java
- Different Ways to Print First K Characters of the String in Java
- Different Ways to Generate String by using Characters and Numbers in Java
- Number of ways to remove a sub-string from S such that all remaining characters are same
- Reverse string in Python (5 different ways)
- Count different numbers possible using all the digits their frequency times
- Different ways of Method Overloading in Java
- Different ways of Reading a text file in Java
- 3 Different ways to print Exception messages in Java
- Different ways to create objects in Java
- Different Ways to Copy Content From One File to Another File in Java
- Different Ways To Check Java Version In Windows
- Ways to remove one element from a binary string so that XOR becomes zero
- Different ways to hide Action bar in Android with Examples
- Array of Strings in C++ (5 Different Ways to Create)
- Remove all non-alphabetical characters of a String in Java
- How to remove all non-alphanumeric characters from a string in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

