简体   繁体   English

替换Java中的子字符串

[英]Replacing Substring in Java

I have situation in Java where I am reading contents of file in String. 我在Java中正在读取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</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: 另一方面,如果您只想摆脱时间戳记的value标签,则可以使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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