简体   繁体   中英

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?

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>");
S = S.replaceAll("<name>timestamp</name>[^<]*<value>[^<]*</value>", "");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM