YunOS交流群欢迎加入YunOS交流群

有关对字符串的处理,需要用到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

记录每一次的成长~~~

posted @ 2014-05-04 17:07  zzuIvy  阅读(549)  评论(0编辑  收藏  举报
YunOS交流群欢迎加入YunOS交流群