2016-02-13 23 views
0

我有一個List<String>其中包含值如Char:Char_Number1_Number2。 我想找到索引替換值,通過搜索Number1_Number2,組合Number1_Number2始終是唯一的。列表<String>查找並替換字符串的一部分值

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 

我想這些任務

  1. 如何找到索引或使用字符串的後半部分的完整值
  2. 如何更換或使用字符串下半年

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

參考

How to replace some particular string in a list of type string using linq

How to replace list item in best way

刪除
一個特定的值

C#: Update Item value in List

+0

** B:S_4_0 **將被替換爲** B:S_5_1 ** ,,但是搜索條件是** * 4_0 * –

+0

'num_num'不能轉換爲int。你是否想把它變成字符串鍵? –

回答

2

這將B:S_5_1取代B:S_4_0,serching爲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 
1

查找指數(返回-1如果沒有找到):

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

替換:

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

刪除:

list.RemoveAt(index); 
2
int index = list.FindIndex(i => i.Contains("4_0")); 

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

希望這將有助於:)