开发者

How to replace value in string array

开发者 https://www.devze.com 2023-01-22 12:46 出处:网络
How to remove my value in String Array and how i c开发者_如何学Goan rearrange public string[] selNames = new string[5];

How to remove my value in String Array and how i c开发者_如何学Goan 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 }


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


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());
0

精彩评论

暂无评论...
验证码 换一张
取 消