2

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+", " "));
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Sanvi
  • 75
  • 2
  • 9

3 Answers3

9

If you have non-standard spaces in your text, such as characters from Unicode categories:

use this:

str = str.replaceAll("[\\s\\p{Z}]+", " ").trim();

where \s matches whitespace characters ([ \t\n\x0B\f\r]), and \p{Z} is shorthand for \p{Zs}\p{Zp}\p{Zl} as listed above.

It will basically replace all whitespace and separator characters into spaces, collapse consecutive spaces into a single space, and remove leading and trailing spaces.

Andreas
  • 154,647
  • 11
  • 152
  • 247
0
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.replaceAll("\\s+", " ");
    System.out.println(str);
}

Output:

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
Cory Owens
  • 327
  • 2
  • 11
0

use StringUtils.normalizeSpace(str);

Output:

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
Narasimha A
  • 319
  • 5
  • 14