Eka Cahya Pratama Eka Cahya Pratama - 1 year ago 115
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 Source

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>", "" )
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download