Can someone kindly suggest a succinct/elegant way to trim leading and trailing punctuation characters (Java regex \p{Punct}
or POSIX regex [:punct:]
) from a Java string? Punctuation characters that are within the string "body" (i.e. between the first and the last non-punctuation characters in the string) are to be retained.
I did get it kinda working by converting the String to a character array and looping in the forwards and backwards direction and skipping over the punctuation characters, until the first non-punctuation character is encountered. However, I feel that there has to be a better way to do it.