Spen D
Spen D

Reputation: 4345

How to replace value in string array

How to remove my value in String Array and how i can rearrange

public string[] selNames = new string[5];
selNames[0]="AA";
selNames[1]="BB";
selNames[2]="CC";
selNames[3]="DD";
selNames[4]="EE";

In certain Conditaion i need to Check for the existing value and i want to remove it from my collection, How i can do it.

i tried like below, but i cannot, it returns true, but how to make that index value to null

If(selNames .Contains("CC").ToString()==true)

{ // how to make that index null which contains the "CC"; and i need to rearrage the array }

Upvotes: 0

Views: 3750

Answers (2)

Danil
Danil

Reputation: 1893

You can do following.

var newArray = selNames.Where(s => s != "CC").ToArray();

where s is the arg of the Func<TSource, bool> delegate TSource is string in your case. So it will compare each string in array and return all which is not "СС"

here is a link to msdn

Upvotes: 3

Shivkant
Shivkant

Reputation: 4619

You can use the 'List< T >' for checking the existing values and also can remove the item from the list and also can arrange the list. The following is the code snippet:

 List<string> list = new List<string>();
 list.Add("AA");
 list.Add("BB");
 list.Add("CC");
 list.Add("DD");
 list.Add("EE");
 list.Add("FF");
 list.Add("GG");
 list.Add("HH");
 list.Add("II");

 MessageBox.Show(list.Count.ToString());
 list.Remove("CC");
 MessageBox.Show(list.Count.ToString());

Upvotes: 2

Related Questions