This will remove all digits:

firstname1 = firstname1.replaceAll("\\d","");
Answer from jlordo on Stack Overflow
🌐
imalittletester
imalittletester.com › 2017 › 09 › 12 › removing-all-digits-non-digits-whitespaces-from-strings-and-other-usages-of-replaceall
Removing all digits, non-digits, whitespaces (from Strings) and other usages of replaceAll | imalittletester
April 12, 2020 - A good list of regex’s that are usable with this method can be found here: https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html. For the given String, the replaceAll command using the \d pattern will be used to remove all ...
🌐
Codingface
codingface.com › home › java › java string programs › 3 simple ways to remove numbers from string java
3 Simple Ways To Remove Numbers From String Java
June 5, 2021 - So to remove this number 123, we have used replaceAll( ) method as str.replaceAll(“[0123456789]”, “”);. It can replace all numbers including 123 from the given String. You can take any example of String to test this program. Finally, we have displayed the given String without numbers. Also Read: How To Check String Contains Special Characters In Java?
🌐
Simplesolution
simplesolution.dev › java-replace-or-remove-all-digits-of-a-string-in-using-regular-expression
Replace or Remove All Digits of a String in Java using regular expression
Java code example using regex “[0-9]” with String.replaceAll() method · public class RemoveReplaceDigitsExample1 { public static void main(String[] args) { String testString = "Your OTP (one time password) is 112233"; // Remove all numeric ...
🌐
LabEx
labex.io › tutorials › java-removing-numeric-values-from-java-strings-117447
Java Programming | Remove Numeric Values from Strings | LabEx
In this lab, we have learned how to remove numeric values from a String in Java. We have used the replaceAll() method along with different Regex to filter the String and get only the alphabets.
🌐
Alvin Alexander
alvinalexander.com › java › java-strip-characters-string-letters-numbers-replace
Java: How to strip unwanted characters from a string | alvinalexander.com
Here's a quick line of Java code that takes a given input string, strips all the characters from that string other than lowercase and uppercase letters, and returns whatever is left: String newString = aString.replaceAll("[^a-zA-Z]",""); As you might be able to tell by looking at that line of code, the String replaceAll method does the hard work for you, essentially removing the characters in the output stream that you want to remove as it converts the String named aString into a new String named newString.
🌐
CodeSpeedy
codespeedy.com › home › java program to remove all numbers from a string.
Java Program to Remove all Numbers from a string.
June 25, 2019 - which can be used to replace all values of the variable “to_be_replaced” with the values of “replace_with” ... import java.util.Scanner; class deletion { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String inp; // Variable 'inp',for storing the input System.out.println("Enter your string:"); inp = sc.nextLine(); System.out.println("After deletion of any digits,the string is:"); inp = inp.replaceAll("[0123456789]",""); // .replaceAll(),function used to replace.
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java string › retain only digits and decimal separator in string
Retain Only Digits and Decimal Separator in String | Baeldung
January 18, 2024 - In Apache Commons, the RegExUtils class provides a straightforward method removeAll(String text, String regex) to remove all characters that fit the criteria specified in the regex.
🌐
Baeldung
baeldung.com › home › java › java string › how to remove digits from a string
How to Remove Digits From a String | Baeldung
June 15, 2024 - To remove digits from a String, we need a regex pattern that identifies digits. In regex, \d matches any digit (0-9). By using \d, we can target all the digits in a String and replace them with an empty String.
🌐
Coderanch
coderanch.com › t › 381911 › java › remove-Numeric-values-String
How to remove Numeric values from a String (Java in General forum at Coderanch)
January 25, 2007 - Please send your replies as soon as possible. Thanks in Advance. ... Look around in the API documentation of the classes String and Character. If you have a string and you want to remove all the digits from it, you could write a loop to go over all the characters in the string.
🌐
TutorialsPoint
tutorialspoint.com › java-program-to-remove-all-numbers-in-a-string-except-1-and-2
Java program to remove all numbers in a string except "1" and "2"?
October 1, 2020 - import java.util.Scanner; public class RegexExample { public static void main(String args[]) { //Reading String from user System.out.println("Enter a String"); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); //Regular expression to match the digit 1 String regex1 = "(?<!\d)1(?!\d)"; //Regular expression to match the digit 2 String regex2 = "(?<!\d)2(?!\d)"; //Replacing all space characters with single space String result = input.replaceAll(regex1, "one") .replaceAll(regex2, "two") .replaceAll("\s*\d+", ""); System.out.print("Result: "+result); } } Enter a String sample 1 2 3 4 5 6 Result: sample one two ·
🌐
YouTube
youtube.com › just engineers thing
Java Program to Remove Digits from a String | Tcs, Infosys, Cognizant Frequently Asked Problem 😲 - YouTube
Remove Digits from a String in 2 simple ways. This is a Frequently asked Java program in MNC.If you like this video Don't forget to Subscribe.
Published   April 26, 2022
Views   10K
🌐
GeeksforGeeks
geeksforgeeks.org › java › different-ways-to-remove-all-the-digits-from-string-in-java
Different Ways to Remove all the Digits from String in Java - GeeksforGeeks
July 15, 2025 - 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.
🌐
Interview Kickstart
interviewkickstart.com › home › blogs › learn › how to remove all non-alphanumeric characters from a string in java?
Remove Non-Alphanumeric Characters from a String in Java
December 19, 2024 - Task: To remove all non-alphanumeric characters in the given string and print the modified string. ... How to Remove Non-alphanumeric Characters in Java: Method 1: Using ASCII values Method 2: Using String.replace() Method 3: Using String.replaceAll() and Regular Expression · Alpha stands for alphabets, and numeric stands for a number.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-remove-all-non-alphanumeric-characters-from-a-string-in-java
How to remove all non-alphanumeric characters from a string in Java - GeeksforGeeks
July 15, 2025 - The approach is to use the String.replaceAll method to replace all the non-alphanumeric characters with an empty string. Below is the implementation of the above approach: ... // Java program to remove non-alphanumeric // characters from a string ...
🌐
HCL GUVI
studytonight.com › java-examples › how-to-remove-numeric-values-from-string
HCL GUVI | Learn to code in your native language
Take your tech career to the next level with HCL GUVI's online programming courses. Learn in native languages with job placement support. Enroll now!