I currently struggling trying to extract a certain substring of a string and replacing it's content. The digits to be exact. The numbers change, so I can not take the whole string. For example, I have a message like
String message = "hi John. You have two contracts listed:
TV-70002548 and FV-50006578. The first contract
ends on March 15th. I will pay you a final amount of 350 $."
In that String I have a set of different numbers, but I only want to extract the contract numbers and have them rewritten, like TV-70002548 -> TV-seven null null null two five four eight.
I wrote a method that extracts all digits after V- because this character is unique for the contract numbers in a string:
private String replaceContractNo(String pMessage, String pInput, String pOutput) {
String contractno;
contractno = pMessage.substring(pMessage.indexOf("V-")-2);
return contractno.replaceAll(pInput, pOutput);
}
Later I want to replace the digits with words, like:
replaceContractNo(pMainMessage,"0"," null")
replaceContractNo(pMainMessage,"1"," one")
This does not work, because the message gets written out each time. Is there an easy solution to this problem?
Thank you