How to replace Digits into String using Java?

In Java, you can replace digits in a string with their corresponding word representations using a HashMap to map each digit to its text equivalent. This approach allows you to convert numeric characters to readable words efficiently.

Creating the Digit-to-Word Mapping

First, create a HashMap object from the java.util package to store digit-to-word mappings ?

Map<String, String> map = new HashMap<String, String>();

This HashMap associates each digit with its corresponding word representation ?

map.put("0", "zero");
map.put("1", "one");
map.put("2", "two");
// ... and so on for all digits

Algorithm Steps

The process involves iterating through each character in the string, checking if it's a digit using the HashMap's containsKey() method, and replacing it with the corresponding word or keeping the original character.

Complete Example

import java.util.*;

public class DigitReplacer {
    public static void main(String args[]) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("0", "zero");
        map.put("1", "one");
        map.put("2", "two");
        map.put("3", "three");
        map.put("4", "four");
        map.put("5", "five");
        map.put("6", "six");
        map.put("7", "seven");
        map.put("8", "eight");
        map.put("9", "nine");
        
        String s = "I have 3 Networking books, 0 Database books, and 8 Programming books.";
        String newstr = "";
        
        for (int i = 0; i < s.length(); i++) {
            String k = s.substring(i, i + 1);
            if (map.containsKey(k)) {
                String v = map.get(k);
                newstr = newstr + v;
            } else {
                newstr = newstr + k;
            }
        }
        
        System.out.println("Original: " + s);
        System.out.println("Modified: " + newstr);
    }
}

Output

Original: I have 3 Networking books, 0 Database books, and 8 Programming books.
Modified: I have three Networking books, zero Database books, and eight Programming books.

How It Works

The algorithm extracts each character using substring(i, i+1), checks if it exists as a key in the HashMap using containsKey(), and either appends the corresponding word value or the original character to build the new string.

Alternative Using StringBuilder

For better performance with large strings, use StringBuilder instead of string concatenation ?

StringBuilder newstr = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
    String k = s.substring(i, i + 1);
    if (map.containsKey(k)) {
        newstr.append(map.get(k));
    } else {
        newstr.append(k);
    }
}
System.out.println(newstr.toString());

Conclusion

Using HashMap with character iteration provides an efficient way to replace digits with words in Java. The StringBuilder approach is recommended for better performance when processing large strings.

Updated on: 2026-03-15T19:13:30+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements