I am unable to strip one space before and after a hyphen. I have tried: –
sample.replaceAll("[\\s\\-\\s]","")
and permutations to no avail. I dont want to strip all spaces, neither all the intervening spaces. I am trying to parse a string based on " " but want to eliminate "-". Any insight appreciated.
[\s\-\s]is a character class, and does not matchesspace followed by - followed by space. It matches any of the characters –space, and-, and replace them withempty string.You can use this: –
Or, even
String.replacewould work here. You don’t really need areplaceAll: –