-2

Am trying to remove the spaces from the java string leaving the special characters,...

existing: "[Cars]/Info/{​​​​​​​​​      123}​​​​​​​​​    /{4533449​​​​​​​​​      }​     ";
expected: "[Cars]/Info/{​​​​​​​​​123}​​​​​​​​​/{4533449​​​​​​​​​}";

Any help would be really appreciated..

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140

1 Answers1

1

You should use .replace(toReplace, replaceWith) like so:

st.replaceAll("\\s+","")

Where st is "[Cars]/Info/{​​​​​​​​​ 123}​​​​​​​​​ /{4533449​​​​​​​​​ }​ "

This:

String st = "[Cars]/Info/{​​​​​​​​​      123}​​​​​​​​​    /{4533449​​​​​​​​​      }​     ";
System.out.println(st.replaceAll("\\s+",""));

Produces this:

[Cars]/Info/{​​​​​​​​​123}​​​​​​​​​/{4533449​​​​​​​​​}​

If you want to remove the spaces for the specific variable, use

st = st.replaceAll("\\s+","")
Spectric
  • 30,714
  • 6
  • 20
  • 43