-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..

YCF_L
  • 51,266
  • 13
  • 85
  • 129

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
  • 27,594
  • 6
  • 14
  • 39