Let's Say i have following array:
string[] myArray = new string[6]; myArray[0] = "a"; myArray[1] = "b"; myArray[2] = " "; //space myArray[3] = "c"; myArray[4] = ""; //null myArray[5] = "d";
I want to remove the space and null parts from the array.
so after trimming the array should be like this:
myArray[0] = "a"; myArray[1] = "b"; myArray[2] = "c"; myArray[3] = "d";
Code:
public string[] TrimStringArray(string[] strArray) { int c = 0; foreach (string str in strArray) { if (str != "" && str != null) { c++; } } string[] tempArrary = new string[c]; int j=0; for (int i = 0; i < strArray.Length; i++) { if( strArray[i] != null && strArray[i] != "") { tempArrary[j] = strArray[i]; j++; } } return tempArrary; }
Examle:
myArray = TrimStringArray(myArray);
原文链接: https://www.cnblogs.com/samblog/archive/2012/04/01/2429325.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/45875
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!