I have a string
"This is a big sentence . ! ? ! but I have to remove the space ."
In this sentence I want to remove all the space coming before the punctuation and should become
"This is a big sentence.!?! but I have to remove the space."
I am trying to use "\p{Punct}"
but not able to replace in string.
You should use positive lookahead:
newStr = str.replaceAll("\\s+(?=\\p{Punct})", "")
ideone.com demo for your particular string
Break down of the expression:
\s
: White space...(?=\\p{Punct})
... which is followed by punctuation.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With