簡體   English   中英

名單 <String> 查找並用字符串的一部分替換值

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

我有一個List<String> ,其中包含類似於Char:Char_Number1_Number2值。 我想查找索引替換值 ,通過搜索Number1_Number2Number1_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

我要這些任務

  1. 如何使用字符串的后半部分查找索引或完整值
  2. 如何使用字符串的后半部分替換或刪除特定值

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

參考

如何使用linq替換類型字符串列表中的某些特定字符串

如何以最佳方式替換列表項

C#:更新列表中的項目值

這會將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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM