現在、特定の文字列から特定の文字列を削除しようとしています。ループを使用して特定の文字列を文字列から削除する
public static String Replace(String input) {
String step1 = input.replace("List of devices attached", "");
String step2 = step1.replace("* daemon not running. starting it now on port 5037 *", "");
String step3 = step2.replace("* daemon started successfully *", "");
String step4 = step3.replace(" ", "");
String step5 = step4.replace("device", "");
String step6 = step5.replace("offline", "");
String step7 = step6.replace("unauthorized", "");
String finished = step7;
return finished;
}
このアウトプット::私はそれは以下のように複数の変数を使用して行っている
5VT7N16324000434
イムアレイと、このような種類のループを使用して、これを短縮することができな方法があるかどうか迷っ:
public static String Replace(String input) {
String[] array = {"List of devices attached",
"* daemon not running. starting it now on port 5037 *",
"* daemon started successfully *",
" ",
"device",
"offline",
"unauthorized"};
for (String remove : array){
input.replace(remove, "");
}
String output = input;
return output;
両方を実行した後、最初の例は必要な処理を行いますが、2番目の例は実行しません。出力:
List of devices attached
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
List of devices attached
5VT7N16324000434 device
私の2番目の例は可能ですか?それはなぜ機能しないのですか?
ありがとうございました。私は変数を宣言するのを忘れていた –