List<string> list = new List<string>() { "218.173.0.0:218.213.255.255" }; Dictionary<string, List<string>> ipSet = new Dictionary<string, List<string>>(); List<string> temp = new List<string>(); foreach (string ipArea in list) { temp.Clear(); string[] ips = ipArea.Split(':')[0].Split('.'); string[] ipe = ipArea.Split(':')[1].Split('.'); for (int m = int.Parse(ips[0]); m <= int.Parse(ipe[0]); m++) { for (int n = int.Parse(ips[1]); n <= int.Parse(ipe[1]); n++) { for (int i = int.Parse(ips[2]); i <= int.Parse(ipe[2]); i++) { for (int j = int.Parse(ips[3]); j <= int.Parse(ipe[3]); j++) { temp.Add(string.Format("{0}:{1}:{2}:{3}", m, n, i, j)); } } } } ipSet.Add(ipArea, temp); }
如果你没有自己定义Type的概念,那就没有办法了。如果有,那么你可以写(例如):public static List<MyLib> lib1; public static SortedSet<MyLib> Lib2;public class MyLib { public long From; public long To; public Area; } 而不要只会抱着基本的数据。但愿这对你来说不是对牛弹琴。
ipv4虽然只有32位,但理论上也有40多亿个,你全放在字典里显然是不太现实。
List<string> list = new List<string>() { "218.173.0.0:218.213.255.255" };
Dictionary<string, List<string>> ipSet = new Dictionary<string, List<string>>();
List<string> temp = new List<string>();
foreach (string ipArea in list)
{
temp.Clear();
string[] ips = ipArea.Split(':')[0].Split('.');
string[] ipe = ipArea.Split(':')[1].Split('.');
for (int m = int.Parse(ips[0]); m <= int.Parse(ipe[0]); m++)
{
for (int n = int.Parse(ips[1]); n <= int.Parse(ipe[1]); n++)
{
for (int i = int.Parse(ips[2]); i <= int.Parse(ipe[2]); i++)
{
for (int j = int.Parse(ips[3]); j <= int.Parse(ipe[3]); j++)
{
temp.Add(string.Format("{0}:{1}:{2}:{3}", m, n, i, j));
}
}
}
}
ipSet.Add(ipArea, temp);
}
[1]=>{"2323",
"23432",
"344334",
"435445"}
[2]->{"1111",
"2222",
"3333"}
.....
这样就可以先随机取到[1],然后再随机取[1]里面的字符串
"深圳"=>"222.14.0.0:222.17.255.255",
"19.147.0.0:19.187.255.255",
"107.19.0.0:107.20.255.255"
"北京"=>"212.18.0.0:212.19.255.255",
"20.147.0.0:20.187.255.255",
"117.19.0.0:117.20.255.255"
list.Add(1,new List<string>(){ "2323","23432","344334","435445" });
list.Add(2,new List<string>(){ "1111","2222","3333" });
如果你没有自己定义Type的概念,那就没有办法了。如果有,那么你可以写(例如):public static List<MyLib> lib1;
public static SortedSet<MyLib> Lib2;public class MyLib
{
public long From;
public long To;
public Area;
}
而不要只会抱着基本的数据。但愿这对你来说不是对牛弹琴。