Eka Cahya Pratama Eka Cahya Pratama - 3 months ago 17
Java Question

Remove double tag from string

I have a String look like this :

<start> <start> some sentence <stop> is a sentence <stop>


How I can make those String something like this :

<start> some sentence is a sentence <stop>


So far I'm using regex to remove the double start first

string.replace("<start> <start>","<start>");


but I'm still stuck at removing the middle stop tag.

Answer

Can do like below replaceFirst is String class method which replaces the first substring of this string that matches the given regular expression with the given replacement.

String finalResult=string.replaceFirst("<start>", "" ).replaceFirst("<stop>", "" )