Question

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

How can I do it? Thanks

Was it helpful?

Solution

If you have multiple IP Address in your string separated by comma , then you can do:

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 tempIP = null;
List<IPAddress> safeList = ip.Split(',')
                             .Where(r => IPAddress.TryParse(r, out tempIP))
                             .Select(r => tempIP)
                             .ToList();

OTHER TIPS

Try

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top