[英]How to create a list<IPAddress> from string
I have a string of IP addresses string ip = "123.37.71.238,123.37.71.239"
(It is a example, actually it has around 100 addresses) Need to generate list of this string我有一串 IP 地址
string ip = "123.37.71.238,123.37.71.239"
(这是一个例子,实际上它有大约 100 个地址) 需要生成这个字符串的列表
How can I do it?我该怎么做? Thanks
谢谢
If you have multiple IP Address in your string separated by comma ,
then you can do:如果您的字符串中有多个 IP 地址以逗号分隔
,
那么您可以执行以下操作:
string ip = "123.37.71.238,123.37.71.239";
List<IPAddress> ipAddressList = ip.Split(',')
.Select(IPAddress.Parse)
.ToList();
If you want to use IPAddress.TryParse
then you can do:如果你想使用
IPAddress.TryParse
那么你可以这样做:
IPAddress tempIP = null;
List<IPAddress> safeList = ip.Split(',')
.Where(r => IPAddress.TryParse(r, out tempIP))
.Select(r => tempIP)
.ToList();
尝试
var list = ip.Split(',').Select(n => IPAddress.Parse(n)).ToList();
A possible solution...一个可能的解决方案...
ip.Split(",");
Then you'll have an array of IP address strings, which you can then call ToList() on.然后您将拥有一组 IP 地址字符串,然后您可以对其调用 ToList()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.