---
title: "Replacement of English numbers of a string with Roman numbers using java"  
description: "Replacement of English numbers of a string with Roman numbers using java"  
author: "Anonymous User"  
published: 2013-10-14  
updated: 2013-10-14  
canonical: https://www.mindstick.com/forum/1664/replacement-of-english-numbers-of-a-string-with-roman-numbers-using-java  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Replacement of English numbers of a string with Roman numbers using java

I'm going to find number chars in a [String](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) and [replace](https://yourviews.mindstick.com/view/81330/cow-antibody-research-in-usa-a-step-ahead-to-replace-plasma-therapy) them with their Roman versions. The [Code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) is:

```
public static void main(String[] args) {
    String pattern = "[0-9]+";
    Pattern p = Pattern.compile(pattern);
    String mainText = "34titi685dytti5685fjjfj8585443";
    Matcher m = p.matcher(mainText);
    int i = 0;
    while (m.find()) {
        System.out.println("Match number " + i);
        String tmp = m.group();
        char[] cTmp = tmp.toCharArray();
        for (int j = 0; j < cTmp.length; j++) {
            cTmp[j] = (char) ((int) cTmp[j] + 1584);
        }
        m.group().replaceFirst(tmp,new String(cTmp));
        i++;
    }
    System.out.println(mainText);
}
```

\

But at the [end](https://yourviews.mindstick.com/view/88503/us-iran-war-live-updates-trump-says-us-in-no-rush-to-end-iran-war) it prints the same string main [text](https://www.mindstick.com/blog/301635/did-people-reinvent-texting-to-express-the-full-range-of-emotions). What is [wrong](https://answers.mindstick.com/qa/48468/who-wrote-the-the-wrong-enemy-america-in-afghanistan-2001-2014-and-when) with my code?

## Replies

### Reply by Anonymous User

This is not how you do a replacement using Matcher. m.group() just gives you the matched part of the string. Whatever replacement you do in it, you have to perform concatenation with original string. This is due to the fact that Strings are immutable objects. You don't perform in-place replacement to it.

## You do it like this:

```
StringBuffer buffer = new StringBuffer();
while (m.find()) {
    String tmp = m.group();
    char[] cTmp = tmp.toCharArray();
    for (int j = 0; j < cTmp.length; j++) {
        cTmp[j] = (char) (cTmp[j] + 1584);  // You don't need to typecast `cTmp[j]` to `int`.
    }
    m.appendReplacement(buffer, new String(cTmp));
}
m.appendTail(buffer);
System.out.println(buffer.toString());
```

\

\


---

Original Source: https://www.mindstick.com/forum/1664/replacement-of-english-numbers-of-a-string-with-roman-numbers-using-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
