-3

I have string in which more than one underscore can be present together.

For example : this_is__Dummy_____String

I have to replace this more than one occurance by only one underscore so that target string should look like :

this_is_Dummy_String

Thanks in advance !

DevX
  • 490
  • 6
  • 23

1 Answers1

3

You can use String#replaceAll to replace the undescores.

"this_is__Dummy_____String".replaceAll("_{2,}", "_")

The given regex will replace all occurences of "two or more" underscores with a single underscore.

ST-DDT
  • 2,615
  • 3
  • 30
  • 51