So I have a string like this: <em>1234</em>56.70
it's basically a number where the em
tags help identify what to highlight in the string
I need to first convert the string to an actual number with the current locale format. So I remove the em
tags (replaceAll by emptyString) and then use the numberFormat
java API to get a string like: $123,456.70
The problem with this is, I lost the highlight (em
) tags. So I need to put it back in the string that is formatted, something like this: <em>$123,4</em>56.70
highlightValue = "<em>1234</em>56.70";
highlightValue = highlightValue.replaceAll("<em>", "").replaceAll("</em>", ""); // highlightValue is now 123456.70
highlightValue = numberFormat.convertToFormat(highlightValue, currencyCode); // highlightValue is now $123,456.70
highlightValue = someFunction(highlightValue); // this function needs to return <em>$123,4</em>56.70
I am not sure what approach to use. I was trying pattern matching but didn't know how to achieve it.
All help appreciated !
I am assuming that you want to highlight the number from starting up to some number of digits.This can be done.
In the initial string count the number of digits after which the tag is present. The starting tag will always be placed at the beginning. It is the ending tag you have to worry about. Now count the number of digits, excluding any other symbols.When the required number of digits have been passed, again place the tag. Either you can create a StringBuilder
from the String highlighted
and insert the tag string directly, or divide the string into two substrings and then join them together with the tag string in the middle.
Hope this helped.