Replace all Digits in a String with a Specific Character in Java Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 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 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 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 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 How to remove all non-alphanumeric characters from a string in Java Given string str, the task is to remove all non-alphanumeric characters from it and print the modified it. Examples: Input: @!Geeks-for'Geeks,123 Output: GeeksforGeeks123 Explanation: at symbol(@), exclamation point(!), dash(-), apostrophes('), and commas(, ) are removed.Input: Geeks_for$ Geeks?{}[] 3 min read Like