Different Ways to Remove all the Digits from String in Java
Last Updated :
03 Oct, 2022
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));
}
}
- Time Complexity: O(N)
- Auxiliary Space: 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));
}
}
- Time Complexity: O(N)
- Auxiliary Space: O(N)
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));
}
}
- Time Complexity: O(N)
- Auxiliary Space: O(1)
Similar Reads
Move all digits to the beginning of a given string Given a string S, the task is to move all the digits present in the string, to the beginning of the string. Examples: Input: S = âGeeks4forGeeks123âOutput: 4123GeeksforGeeksExplanation:The given string contains digits 4, 1, 2, and 3. Moving all the digits to the beginning of the string modifies the
7 min read
Remove Leading Zeros From String in Java Given a string of digits, remove leading zeros from it.Illustrations: Input : 00000123569Output: 123569Input: 000012356090Output: 12356090Approach: We use the StringBuffer class as Strings are immutable.Count leading zeros by iterating string using charAt(i) and checking for 0 at the "i" th indices.
3 min read
Different Ways to Generate String by using Characters and Numbers in Java Given a number num and String str, the task is to generate the new String by extracting the character from the string by using the index value of numbers. Examples: Input: str = âGeeksforGeeksâ num = 858 Output: GfG Explanation: The 8th, 5th, and 8th position of the characters are extracting from th
3 min read
Replace all Digits in a String with a Specific Character in Java To replace all digits in a string with a specific character in Java, we can use the regular expressions and the replaceAll() method of the String class. This method allows to search for patterns in the string and replace them with a given character or substring.Example 1: The below Java program demo
3 min read
Remove leading zeros from a Number given as a string Given numeric string str, the task is to remove all the leading zeros from a given string. If the string contains only zeros, then print a single "0". Examples: Input: str = "0001234" Output: 1234 Explanation: Removal of leading substring "000" modifies the string to "1234". Hence, the final answer
7 min read