2

Given a word-string in Java, I want to strip off from beginning and from end, exactly these specified set of characters:

[?:!.,;'\"«»]

as many times as they appear.

For instance, «Be!!» should become just Be, "Here!!!" should become Here, «I should become I.

Can anyone provide a correct way to do this?

Leonardo
  • 317
  • 2
  • 5
  • 11

1 Answers1

4

Use an anchored regex in string.replaceAll function.

string.replaceAll("^[?:!.,;'\"«»]+|[?:!.,;'\"«»]+$", "");

DEMO

Avinash Raj
  • 166,785
  • 24
  • 204
  • 249