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