use of regular expressions in string.replaceAll
I have a String in which I'm trying to replace all individual characters (except for spaces) surrounded by spaces with hyphens.
I thought the code to do this would look something like this:
Code :
str = str.replaceAll(" ^\\s ", " - ");
However, this doesn't work, so if someone has a solution, it'd be much appreciated.
Re: use of regular expressions in string.replaceAll
Try using negation of a character class...' [^\\s] '
Re: use of regular expressions in string.replaceAll
Could you please provide the exact line of code you are thinking? I don't know if those single quotes are to be in the code, or where I'm supposed to use them. Thanks.
Re: use of regular expressions in string.replaceAll
I was single quoting the regular expression :
Code :
str = str.replaceAll(" [^\\s] ", " - ");