Question

Im reading file line by line and i want to convert some value to integer. One of the line of file is 92.44.12.5/28 and i want to convert 5 to integer. Im able to convert until the 12 but it didnt convert to 5 .

string[] ip1 = line.Split('.');
int [] ipArray = Array.ConvertAll(ip1[2].Split(),Int32.Parse);

what is wrong with my code?

Was it helpful?

Solution 2

A simplest fix for your code is (this will convert all your literals):

string[] ip1 = "92.44.12.5/28".Split('.', '/');
int [] ipArray = Array.ConvertAll(ip1,Int32.Parse);

Console.WriteLine(ipArray[3]);

OTHER TIPS

You can use LINQ + int.Parse:

int[] ipArray = lines
    .Select(l => int.Parse(l.Split('/')[0].Split('.').Last())); 
    .ToArray(); 

This presumes that the format is strict, otherwise you get an exception at int.Parse.

Here's a safer version:

int ip = 0;
int[] ipArray = lines
    .Where(l => l.Contains('/') && l.Contains('.'))
    .Select(l => l.Trim().Split('/')[0].Split('.').Last())
    .Where(i => int.TryParse(i, out ip))
    .Select(i => ip)
    .ToArray(); 

If you instead want to find all 4 numbers of all IP's, so one array for every line:

int[][] allIPs = lines
    .Where(l => l.Contains('/') && l.Contains('.'))
    .Select(l => l.Trim().Split('/')[0].Split('.'))
    .Where(split => split.Length == 4 && split.All(str => str.All(Char.IsDigit)))
    .Select(split => Array.ConvertAll(split, int.Parse))
    .ToArray(); 

Note that this is not IPv6 compatible ;)

Use that:

line.Split(new char[] { '.', '/' });

line.Split('.') converts your string into an array containing the following elements: 92, 44, 12, 5/28. The first three will work fine, but not the last one; 5/28 is not a number.

You may want to remove the slash and everything following it before splitting by the period:

line = line.Split('/')[0];
string[] ip1 = line.Split('.');
....

This Code will give you all the integers in a string ;

List<int> integer = new List<int>();
string number="";
foreach(char o in yourString)
{
    try
    {
        Convert.ToInt32(Convert.ToString(o));//try if o == an integer
        number+=o;
    }
    catch
    {
        if(number!="")
        {
            integer.Add(Convert.ToInt32(number));
            number="";
        }
    }
}

That's the shortest I could think of:

int[] ipArray = "92.44.12.5/28".Split('.', '/').Select(int.Parse).ToArray();

I can't add any improvement on the other answers, but as an alternative, there is also:

System.Net.IPAddress.Parse("92.44.12.5/28".Split('/').First()).GetAddressBytes().Last()

Please try out below code, it gives output per your expectation

string line = "92.44.12.5/28";
string[] ip1 = line.Split('.');
int[] ipArray = Array.ConvertAll(ip1, new Converter<string, int>(StringToInt));

 public static int StringToInt(string value)
        {
            value = value.Contains('/') ? value.Split('/')[0] : value;  
            int result = 0;
                int.TryParse(value, out result);
            return result;
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top