Removing a word that contains specific word or part of the word from a string

tupac shakur :

I have a string which I want to remove specific words that contains certain word inside of them.

for example: String str = "Create_DateTime, Hello, DateTime, Before" each word that contains the word 'Date' should be removed so after the removal we will get the following string after the removal will be: Hello,Before

I have this string: 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

and I've managed to remove all the unneeded words so now my string looks like that: ,Metrics_Count,Device_UID,Command_Name,Command_Interval,,Instance_UID,,Metrics_Size_KB,,Tags_Countfrom command_execution

I want to remove the ',' before or after the word

This is the code I used to do the above:

String str1 = str.replaceAll("\\w*Date\\w*","");

Original string: 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

Expected:

Metrics_Count,Device_UID,Command_Name,Command_Interval,,Instance_UID,Metrics_Size_KB,,Tags_Countfrom command_execution

Actual: ,Metrics_Count,Device_UID,Command_Name,Command_Interval,,Instance_UID,,Metrics_Size_KB,,Tags_Countfrom command_execution

Arthur :

One approach would be using java streams

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(","));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=311270&siteId=1