-2

I want to replace all special characters in the string shown below:

String a="Test’‵"
    

I want to replace ’ and ‵ with dashes (-). I have tried the following:

a=a.replaceAll("[’|‵]", "-");
    
    

This generates the following result:

Test------

instead of

Test--

How can I achieve the desired result?

iota
  • 34,586
  • 7
  • 32
  • 51
MR AND
  • 356
  • 4
  • 23
  • 7
    Typo? Use `(` `)` instead of `[` `]`. One represents groups, other represents *character class* which can match only *single* character from specified "range". Or don't even use `(..)`, just remove `[` `]`. – Pshemo Jul 21 '20 at 19:31
  • 1
    Before doing anything with regex, you have to solve first your encoding problem (utf-8 => Windows-1252 = correct utf-8 encoding). – Casimir et Hippolyte Jul 21 '20 at 21:27

1 Answers1

1

Don't use square brackets, as it represents a set of single characters to match (a character class).

a=a.replaceAll("’|‵", "-");

Demo!

iota
  • 34,586
  • 7
  • 32
  • 51