[英]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.