我有一个字符串,我想删除其中包含特定单词的特定单词。
例如:
String str = "Create_DateTime, Hello, DateTime, Before"
每个包含单词“Date”的单词都应该被删除,所以在删除之后我们将得到以下字符串:
你好,之前
我有这个字符串:
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
最佳答案
一种方法是使用 java 流
String str = "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";
String result = Stream.of(str.split(","))
.filter(word -> !word.contains("Date"))
.collect(Collectors.joining(","));
关于java - 从字符串中删除包含特定单词或部分单词的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57416621/