Search code examples
javastringtextreplacetextview

How to remove space after the decimal point without removing other spaces?


I have one String that has multiple values in single string. I just want to remove the space after decimal point only without removing other spaces from string.

String testString = "EB:3668. 19KWh DB:22. 29KWh";

testString = testString.trim();
String beforeDecimal = testString.substring(0, testString.indexOf("."));
String afterDecimal = testString.substring(testString.indexOf("."));

afterDecimal = afterDecimal.replaceAll("\\s+", "");

testString = beforeDecimal + afterDecimal;

textView.setText(testString);

Here as in my string there is two values in single string EB:3668. 19KWh and DB:22. 29KWh. I just want to remove space after decimal point and make String like this:

EB:3668.19KWh DB:22.29KWh

Solution

  • Just use string.replaceAll("\\. ", ".");

    Thanks to Henry for pointing out I had to escape the .