[英]List<String> find and replace values by part of string
我有一個List<String>
,其中包含類似於Char:Char_Number1_Number2
值。 我想查找索引或替換值 ,通過搜索Number1_Number2
, Number1_Number2
的組合Number1_Number2
是唯一的。
EG: B:S_4_0將替換為B:S_5_1 ,但搜索條件為4_0
B:S_4_0
B:N_1_2
A:N_3_3
B:N_0_0
A:S_2_5
A:S_3_4
我要這些任務
int index = Player1.IndexOf("5_6"); // Find Index
Player1[index]="5_6"; // Replace value
Player1.Remove("5_6"); // Remove
參考
這會將B:S_4_0
替換為B:S_5_1
,為4_0
List<string> lstString = new List<string> {"B:S_4_0", "B:N_1_2", "A:N_3_3", "B:N_0_0", "A:S_2_5", "A:S_3_4"};
int j = lstString.FindIndex(i => i.Contains("4_0")); //Finds the item index
lstString[j] = lstString[j].Replace("4_0", "5_1"); //Replaces the item by new value
int index = list.FindIndex(i => i.Contains("4_0"));
list[index] = list[index].Replace("4_0", "5_6");
希望這會有所幫助:)
查找索引(如果未找到則返回-1
):
int index = list.FindIndex(s => s.EndsWith("4_0"));
更換:
list[index] = list[index].Replace("4_0", "5_1");
去掉:
list.RemoveAt(index);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.