for (int i = list.Count - 1; i >= 0; i--) { if (list[i].NO == item.NO) { list.RemoveAt(i); } }
public void RemoveItemFromList(ref List <A> list, A item) { List <A> tempList = new List <A>(); foreach (A a in list) { if (a.NO != item.NO && !tempList.Contains(a)) tempList.Add(a); } list = tempList; }
如果支除空元素,可以使用Split參數StringSplitOptions.RemoveEmptyEntries去實現
用法:
string test = "程$曉$"; 使用:string[] temp = test.Split(new string[] { "$" }, StringSplitOptions.RemoveEmptyEntries); 輸出結果:數組長度為2 temp[0]="程" temp[1]="曉"; 使用:string[] temp = test.Split(new string[] { "$" }, StringSplitOptions.None);或string[] temp = test.Split('$'); 輸出結果:數組長度為3 temp[0]="程" temp[1]="曉" temp[2]="";
string[] inputpids = productIds.IndexOf(',') > 0 ? productIds.Split(',').Distinct().ToArray() : new string[] { productIds };
int[] outputpids = Array.ConvertAll<string, int>(inputpids, delegate(string s)
{
var val = 0;
int.TryParse(s, out val);
return val;
});
req.ProductIds = outputpids.Where(c=>c>0).ToList();