1

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.

Sandeep
  • 1,245
  • 1
  • 13
  • 33
  • Ah yes, this is a duplicate. I posted my answer over there instead. – dnault Jun 26 '20 at 02:05
  • @dnault - I thought your answer was better - I retracted my flag. Now [here is a link](https://stackoverflow.com/a/62586907/12567365) to that answer! – andrewJames Jun 26 '20 at 02:07

0 Answers0