zoukankan      html  css  js  c++  java
  • 有关对字符串的处理,需要用到List时的简化写法

    这是项目中的需要根据ComputerName来获取IP的一个方法,如果出现多个ComputerName,需要将多个ComputerName的字符串以“;”分开,传进方法中,然后再处理不同的Name,然后再获取不同Name的IP值,我原来的写法如下:

     1 private static string GetIPAddressFromMachineName(string machineNames)
     2         {
     3             if (machineNames.Trim().EndsWith(";"))
     4             {
     5                 machineNames = machineNames.Trim();
     6                 machineNames = machineNames.Remove(machineNames.LastIndexOf(';'), 1);
     7             }
     8             string result = "";
     9             string[] ipadresses;
    10             try
    11             {
    12                 #region old
    13                 string[] machineName = machineNames.Split(';');
    14                 ipadresses = new string[machineName.Length];
    15                 for (int i = 0; i < machineName.Length; i++)
    16                 {
    17                     if (machineName[i].Trim() != "")
    18                     {
    19                         IPHostEntry ipHose = Dns.GetHostEntry(machineName[i].Trim());
    20                         if (ipHose.AddressList[0].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
    21                         {
    22                             ipadresses[i] = ipHose.AddressList[0].ToString();
    23                         }
    24                         else if (ipHose.AddressList[0].AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
    25                         {
    26                             ipadresses[i] = ipHose.AddressList[1].ToString();
    27                         }
    28                     }
    29                 }
    30                 result = String.Join(",", ipadresses);
    31                 #endregion
    32             }
    33 
    34             catch (Exception)
    35             {
    36                 result = "Error";
    37             }
    38 
    39             return result;
    40         }
    Old

    我们头儿给的建议写法如下:

     1 private static string GetIPAddressFromMachineName(string machineNames)
     2         {
     3             if (machineNames.Trim().EndsWith(";"))
     4             {
     5                 machineNames = machineNames.Trim();
     6                 machineNames = machineNames.Remove(machineNames.LastIndexOf(';'), 1);
     7             }
     8             string result = "";
     9             try
    10             {
    11                 #region new
    12                 foreach (string machineName in machineNames.Split(";".ToArray()).ToList())
    13                 {
    14                     if (machineName.Trim() != "")
    15                     {
    16                         IPHostEntry ipHose = Dns.GetHostEntry(machineName.Trim());
    17                         if (ipHose.AddressList[0].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
    18                         {
    19                             result += "," + ipHose.AddressList[0].ToString();
    20                         }
    21                         else if(ipHose.AddressList[0].AddressFamily==System.Net.Sockets.AddressFamily.InterNetworkV6)
    22                         {
    23                             result += "," + ipHose.AddressList[1].ToString();
    24                         }
    25                     }
    26                 }
    27                 if (result.StartsWith(","))
    28                 {
    29                     result = result.Remove(0, 1);
    30                 }
    31                 #endregion
    32             }
    33 
    34             catch (Exception)
    35             {
    36                 result = "Error";
    37             }
    38 
    39             return result;
    40         }
    New

    记录每一次的成长~~~

  • 相关阅读:
    一个棒棒糖引发的。。。
    做完了一个程序
    C# 串口操作系列(2) 入门篇,为什么我的串口程序在关闭串口时候会死锁 ? .
    MSSQL操作类
    煤矿粉尘监控系统需求分析
    C# 串口操作系列(3) 协议篇,二进制协议数据解析 .
    wp7 手机归属地查询
    .NET设计模式系列文章
    C# 串口操作系列(1) 入门篇,一个标准的,简陋的串口例子。
    常用经典算法
  • 原文地址:https://www.cnblogs.com/zzuIvy/p/stringToListSimplify.html
Copyright © 2011-2022 走看看