私はそれらの内部で特定の単語を含む特定の単語を削除したい文字列を持っています。
例えば: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
一つのアプローチは、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(","));