How to use replaceall to remove all blank space with hypens in apex
why don't you use the replace method and pass in space (' ') & hyphen (-) as parameters. it will replace each space in the string with a hyphen as you expect.
string test = 'this is a space';
string newtest = test.replace(' ','-');
system.debug(newtest);
quick try in dev console anonymous window with debug log
\
is always an escape character.
- To use the regex
\s
, you need to use\\s
. - To use a literal
\
, use\\\\
.