-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
  • 450
  • 4
  • 18

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,371
  • 1
  • 28
  • 43