zoukankan      html  css  js  c++  java
  • String.Join 和 Distinct 方法 去除字符串中重复字符

    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
    RemoveSameIPs

    项目组PM给我做的codeReview,给我做了Code improvement,把这是几行代码,用下面一行代码给替换掉了,真牛掰!

    ipAll = String.Join(",", ipAll.Split(',').Distinct());

    谢谢陈老师!留此纪念!

  • 相关阅读:
    5.2-5.3
    5.1封装
    阅读《构建之法》 5-7章
    做汉堡
    阅读《构建之法》1-5章
    结对 四则运算
    回答
    读后感
    提问*2
    提问1
  • 原文地址:https://www.cnblogs.com/zzuIvy/p/StringJoinAndDistinct.html
Copyright © 2011-2022 走看看