简体   繁体   English

名单 <String> 查找并用字符串的一部分替换值

[英]List<String> find and replace values by part of string

I have a List<String> which contains values like Char:Char_Number1_Number2 . 我有一个List<String> ,其中包含类似于Char:Char_Number1_Number2值。 I want to find the index or replace the value , by searching Number1_Number2 , the combination of Number1_Number2 is always unique. 我想查找索引替换值 ,通过搜索Number1_Number2Number1_Number2的组合Number1_Number2是唯一的。

EG: B:S_4_0 will be replaced as B:S_5_1 , But searching criteria is 4_0 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

I want these tasks 我要这些任务

  1. How to find the index or complete value by using the second half of the string 如何使用字符串的后半部分查找索引或完整值
  2. How to replace or remove a specific value by using the second half of the string 如何使用字符串的后半部分替换或删除特定值

int index = Player1.IndexOf("5_6");    // Find Index
Player1[index]="5_6";                  // Replace value
Player1.Remove("5_6");                 // Remove 

References 参考

How to replace some particular string in a list of type string using linq 如何使用linq替换类型字符串列表中的某些特定字符串

How to replace list item in best way 如何以最佳方式替换列表项

C#: Update Item value in List C#:更新列表中的项目值

This will replace B:S_4_0 by B:S_5_1 , serching for 4_0 这会将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");

Hope this will help :) 希望这会有所帮助:)

Find index (returns -1 if not found): 查找索引(如果未找到则返回-1 ):

int index = list.FindIndex(s => s.EndsWith("4_0"));

Replace: 更换:

list[index] = list[index].Replace("4_0", "5_1");

Remove: 去掉:

list.RemoveAt(index);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM