I have a big conversation, I am handling it as String in between the string there are many white spaces may be invisible non word characters also. Below is an example string:
public static void main(String[] args) {
String str = " TWD day count Spot 6-Sep / 2-Sep 2016 1W7d 13-Sep / 9-Sep 2016 1M30d 6-Oct / 4-Oct 2016 2M62d 7-Nov / 3-Nov 2016 3M91d 6-Dec / 2-Dec 2016 6M181d 6-Mar / 2-Mar 2017 9M273d 6-Jun / 2-Jun 2017 12M365d 6-Sep / 4-Sep 2017 18M546d 6-Mar / 2-Mar 2018 24M730d 6-Sep / 4-Sep 2018";
str = str.toString().replaceAll(" +", "");
System.out.println("str="+str.toString().trim().replaceAll(" ", ""));
}
I tried many string functions to remove white spaces like trim()
, replaceAll(" ","")
, replaceAll("\\s","")
, replaceAll(" +","")
, replaceAll("\\s\\u00a0","")
, stringUtils.normalize()
function etc. Many I tried but not working as expected.
I am expecting the output as below:
"String str = " TWD day count Spot 6-Sep / 2-Sep 2016 1W7d 13-Sep / 9-Sep 2016 1M30d 6-Oct / 4-Oct 2016 2M62d 7-Nov / 3-Nov 2016 3M91d 6-Dec / 2-Dec 2016 6M181d 6-Mar / 2-Mar 2017 9M273d "
Just one space instead of long white duplicate spaces.
Please help.
Found the answer as below:
System.out.println("str="+str.replaceAll("(?U)\\s+", " "));