Remove Digits from String

This program removes digits from a string using regex.

Problem Statement

Write a Java program to remove all digits from a given string.

Source Code

1 public class RegexReplace {
2 public static void main(String[] args) {
3 System.out.println("Eduinq Regex Replace");
4 String str="Eduinq123Test456";
5 String result=str.replaceAll("\\d","");
6 System.out.println("String after removal: "+result);
7 }
8 }

Program Output

Eduinq Regex Replace
String after removal: EduinqTest

Explanation

The regex \d matches any single digit, and replaceAll() replaces every matching digit with an empty string, demonstrating regex-based removal useful for sanitizing input strings.

Quick Links to Explore