Replace all Digits in a String with a Specific Character in Java Last Updated : 10 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 demonstrates replacing all digits in a string with an asterisk (*) using the replaceAll() method. Java // Java program to replace all digits with an asterisk public class Geeks { public static void main(String[] args) { // Declare and initialize the string String s = "Hello123 World456"; // Replace all digits with an asterisk (*) String s1 = s.replaceAll("[0-9]", "*"); System.out.println("Original String: " + s); System.out.println("String after replacing digits: " + s1); } } OutputOriginal String: Hello123 World456 String after replacing digits: Hello*** World*** SyntaxString replaceAll(String regex, String replacement)Parameters:regex: The regular expression to search for in the string. In our case, this will be a pattern to match digits, such as "[0-9]".replacement: The string to replace the matched digits. This could be any character or substring. In this example, we used "*".Return Type: The method returns a new string with the digits replaced by the specified character or substring.Example 2: The below Java program demonstrates replacing all occurrences of the digit '5' in a string with the '@' character using the replaceAll() method. Java // Java program to replace only specific digit 5 with '@' public class Geeks { public static void main(String[] args) { // Declare and initialize the string String s = "My phone number is 555-1234"; // Replace only digit '5' with '@' String s1 = s.replaceAll("5", "@"); System.out.println("Original String: " + s); System.out.println("String after replacing digit 5: " + s1); } } OutputOriginal String: My phone number is 555-1234 String after replacing digit 5: My phone number is @@@-1234 Example 3: The below Java program demonstrates replacing all numbers greater than 100 in a string with the '@' . Java // Java program to replace numbers greater than 100 with '@' public class Geeks { public static void main(String[] args) { // Declare and initialize the string String s = "The numbers are 25, 100, and 200."; // Replace all numbers greater than 100 with '@' String s1 = s.replaceAll("\\b[1-9][0-9]{2,}\\b", "@"); System.out.println("Original String: " + s); System.out.println("String after replacing large numbers: " + s1); } } OutputOriginal String: The numbers are 25, 100, and 200. String after replacing large numbers: The numbers are 25, @, and @. Comment More infoAdvertise with us Next Article String replace() method in Java with Examples J juhisrivastav Follow Improve Article Tags : Java Java-Strings Practice Tags : JavaJava-Strings Similar Reads Replace a character at a specific index in a String in Java In Java, here we are given a string, the task is to replace a character at a specific index in this string. Examples of Replacing Characters in a StringInput: String = "Geeks Gor Geeks", index = 6, ch = 'F'Output: "Geeks For Geeks."Input: String = "Geeks", index = 0, ch = 'g'Output: "geeks"Methods t 3 min read Swapping Characters of a String in Java As we know that Object of String in Java are immutable (i.e. we cannot perform any changes once its created). To do modifications on string stored in a String object, we copy it to a character array, StringBuffer, etc and do modifications on the copy object.In this article we would go through some m 3 min read Java Program to Get a Character from a String Given a String str, the task is to get a specific character from that String at a specific index. Examples:Input: str = "Geeks", index = 2Output: eInput: str = "GeeksForGeeks", index = 5Output: F Below are various ways to do so: Using String.charAt() method: Get the string and the indexGet the speci 5 min read String replace() method in Java with Examples The String replace() method returns a new string after replacing all the old characters/CharSequence with a given character/CharSequence. Example:Return a new string where all " o" characters are replaced with "p" character: Java // Java program to demonstrate // the replace() method public class Ma 4 min read StringBuilder replace() in Java with Examples The replace(int start, int end, String str) method of StringBuilder class is used to replace the characters in a substring of this sequence with characters in the specified String. The substring begins at the specified index start and extends to the character at index end - 1 or to the end of the se 3 min read Different Ways to Remove all the Digits from String in Java 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: GeeksForGeeksExplanation: The given string contains digits 1, 2, and 3. We remove all the digit and prints the 3 min read Like