Qualys项目中写到将ServerIP以“,”分割后插入数据库并将重复的IP去除后发送到服务器进行Scan,于是我写了下面的一段用来剔除重复IP:
1 //CR#1796870 modify by v-yangwu, remove the IPs which are repeated. 2 string[] arrayIP = ipAll.Split(','); 3 List<string> listIP = new List<string>(); 4 foreach (string ip in arrayIP) 5 { 6 if (!listIP.Contains(ip)) 7 { 8 listIP.Add(ip); 9 } 10 } 11 string ipAllNew = listIP[0]; 12 for (int ipaccount = 1; ipaccount < listIP.Count(); ipaccount++) 13 { 14 ipAllNew += "," + listIP[ipaccount]; 15 } 16 ipAll = ipAllNew; 17 //CR#1796870 End
项目组PM给我做的codeReview,给我做了Code improvement,把这是几行代码,用下面一行代码给替换掉了,真牛掰!
ipAll = String.Join(",", ipAll.Split(',').Distinct());
谢谢陈老师!留此纪念!