This will remove all digits:
firstname1 = firstname1.replaceAll("\\d","");
Answer from jlordo on Stack Overflow Top answer 1 of 7
67
This will remove all digits:
firstname1 = firstname1.replaceAll("\\d","");
2 of 7
26
You can use:
firstname1 = firstname1.replaceAll("[0-9]","");
This will remove all numeric values from String firstName1.
String firstname1 = "S1234am";
firstname1 = firstname1.replaceAll("[0-9]","");
System.out.println(firstname1);//Prints Sam
00:59
Remove Special Characters from a String using Regular Expression ...
03:52
Remove special characters from string - YouTube
03:03
Java Program To Remove Special Characters From Given String | Ashok ...
02:45
REMOVE SPECIAL CHARACTERS FROM STRING IN JAVA - YouTube
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?
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.
Top answer 1 of 6
3
The following replaces all whitespace characters (\\s), dots (\\.), and digits (\\d) with "":
name.replaceAll("^[\\s\\.\\d]+", "");
what if I want to replace the
+with_?
name.replaceAll("^[\\s\\.\\d]+", "").replaceAll("\\+", "_");
2 of 6
2
You don't need to escape the ^, you can use \\d+ to match multiple digits, and \\. for a literal dot and you don't need multiple calls to replaceAll. For example,
private static String removeSignsFromName(String name) {
return name.replaceAll("^\\d+\\.", "");
}
Which I tested like
public static void main(String[] args) {
System.out.println(removeSignsFromName("23.Piano+trompet"));
}
And got
Piano+trompet
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.
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
Top answer 1 of 11
622
Try this code:
String str = "a12.334tyz.78x";
str = str.replaceAll("[^\\d.]", "");
Now str will contain "12.334.78".
2 of 11
125
I would use a regex.
String text = "-jaskdh2367sd.27askjdfh23";
String digits = text.replaceAll("[^0-9.]", "");
System.out.println(digits);
prints
2367.2723
You might like to keep - as well for negative numbers.
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.
Codemia
codemia.io › knowledge-hub › path › java_string_remove_all_non_numeric_characters_but_keep_the_decimal_separator
Java String remove all non numeric characters but keep ...
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
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!