【发布时间】:2019-08-08 15:59:31
【问题描述】:
我有一个字符串,我想删除其中包含特定单词的特定单词。
例如:
String str = "Create_DateTime, Hello, DateTime, Before"
每个包含单词“日期”的单词都应该被删除,所以在删除后我们将得到以下字符串:
Hello,Before
我有这个字符串:
Expected_Start_DateTime,Metrics_Count,Device_UID,Command_Name,Command_Interval,Start_DateTime,Instance_UID,Execution_Date,Metrics_Size_KB,End_DateTime,Tags_Countfrom command_execution
我已经设法删除了所有不需要的单词,所以现在我的字符串看起来像这样:
,Metrics_Count,Device_UID,Command_Name,Command_Interval,,Instance_UID,,Metrics_Size_KB,,Tags_Countfrom command_execution
我想删除单词前后的“,”
这是我用来执行上述操作的代码:
String str1 = str.replaceAll("\\w*Date\\w*","");
原始字符串:
Expected_Start_DateTime,Metrics_Count,Device_UID,Command_Name,Command_Interval,Start_DateTime,Instance_UID,Execution_Date,Metrics_Size_KB,End_DateTime,Tags_Countfrom command_execution
预期:
Metrics_Count,Device_UID,Command_Name,Command_Interval,,Instance_UID,Metrics_Size_KB,,Tags_Countfrom command_execution
实际:
,Metrics_Count,Device_UID,Command_Name,Command_Interval,,Instance_UID,,Metrics_Size_KB,,Tags_Countfrom command_execution
【问题讨论】: