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
  • 337
  • 2
  • 5
  • 12

1 Answers1

4

Use an anchored regex in string.replaceAll function.

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

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274