I am trying to remove a searched string from an original string(which is an xml file). For this I used the replaceAll function. However I get empty newlines because I used “” for the string to replace. Is there another way to remove a string?
start =str.indexOf("<opts>");
end =str.indexOf("</opts>");
String removeStr = str.substring(start -6, end + 7);
str = str.replaceAll(removeStr, "");
Tried:
System.out.println("InitialString :="+str);
int start = str.indexOf("<opts>");
int end = str.lastIndexOf("</opts>"); //if \n is added, indent of tag<nos> changes
str = str.substring(0, start ) + str.substring(end + 7, str.length());
System.out.println("FinalString :="+str);
Initial Input String :=
<data>
<param>2</param>
<unit>1</unit>
<opts>
<name>abc</name>
<venue>arena0</venue>
</opts>
<opts>
<name>xyz</name>
<venue>arena1</venue>
</opts>
<nos>100</nos>
</data>
Final Output String :=
<data>
<param>2</param>
<unit>1</unit>
<nos>100</nos>
</data>
You could do it like this;