Faiz Ali Faiz Ali - 1 month ago 8
Java Question

Replacing Substring in Java

I have situation in Java where I am reading contents of file in String. It is something like this -

String S = "<name>source</name> <value>NB_System</value> </nameValue> <nameValue> <name>timestamp</name> <value>2015-6-25 22:39:41:455</value> </nameValue> <nameValue> <name>TTL</name> <value>0</value> </nameValue>"


I want to delete the timestamp from the string -
timestamp</name> <value>2015-6-25 22:39:41:455</value>


Timestamp is creating issues in comparing results with master copy. How to get rid of timestamp here?

Answer

If you want to get rid of the tag timestamp with its value, you can use a code like this:

S = S.replaceAll("<name>timestamp.*?<\/value>", "");

On the other hand, if you just want to get rid of the value tag for the timestamp you could use:

S = S.replaceAll("<name>timestamp.*?<\/value>", "<name>timestamp</name>");
Comments