0

I have a List<String> which contains values like 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.

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

How to replace list item in best way

C#: Update Item value in List

2
  • B:S_4_0 will be replaced as B:S_5_1 ,, But searching Criteria is 4_0 Commented Feb 13, 2016 at 6:55
  • num_num can't be converted to an int. Are you looking to make this into string key? Commented Feb 13, 2016 at 6:58

3 Answers 3

2

This will replace B:S_4_0 by B:S_5_1, serching for 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
Sign up to request clarification or add additional context in comments.

Comments

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

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

Hope this will help :)

Comments

1

Find index (returns -1 if not found):

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

Replace:

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

Remove:

list.RemoveAt(index);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.