Colin
Colin

Reputation: 22595

How to convert a list of strings to ints ignoring non integers

I am used to writing C# without using Linq or Lambda statements, but I'd like to improve my understanding. I have code that looks like this written in C# 2.0 with a foreach loop:

        List<string> strings = new List<string>();
        strings.Add("1");
        strings.Add("blah");

        List<int> ints1 = new List<int>();
        foreach (string x in strings)
        {
            int r;
            if (int.TryParse(x, out r))
                ints1.Add(r);
        }

It's doing a simple task - populating a list of ints from a list of strings, ignoring any that aren't actually ints. In my limited experience, Linq and Lambda statements seem to be able to cut foreach statements down into very succinct and readable ways of doing the same thing. So I thought I'd try this little one using C# 3.0. But the best I could come up with is this:

       IEnumerable<int> ints2 = strings.Select(x =>
                                     {
                                         int r;
                                         if (int.TryParse(x, out r))
                                             return r as int?;
                                         return null;
                                     })
                                     .Where<int?>(x => x != null)
                                     .Select(x => x.Value);

Yuck! I couldn't find a way to combine the converting and filtering in one call and ended up with something that appears to be much worse than the original. Is there a way of doing this or should I stick with my foreach?

EDIT: To summarise findings, here are 2 ways to do this with LINQ if you are using .NET 4.0:

IEnumerable<int> ints1 = strings
.Where(x => new Int32Converter().IsValid(x))
.Select(x => int.Parse(x));//Broken in .NET 3.5 due to bug in Int32Converter


IEnumerable<int> int2 = strings
.Where(x => { int r; return int.TryParse(x, out r); })
.Select(x => int.Parse(x));

The second method is faster and is not broken in .NET 3.5. And there is probably no reason not to put this inside an extension method, as per the accepted answer, instead of the foreach. See connect issue for the Int32Converter bug.

Upvotes: 3

Views: 1908

Answers (5)

Matteo Migliore
Matteo Migliore

Reputation: 923

Another way to get only int using LINQ is:

var integerList =
    strings
        .Where(x => new Int32Converter().IsValid(x))
        .Select(x => int.Parse(x));

[Update]

I did a performance test using LINQPad.

Here I report the code and the result:

void Main()
{
    List<string> strings = new List<string>() { "1", "a", "3", "b" };

    Func<IEnumerable<string>, IEnumerable<int>> func1 =
        list => list.Where(x => new Int32Converter().IsValid(x)).Select(x => int.Parse(x));

    Func<IEnumerable<string>, IEnumerable<int>> func2 =
        list => { var ret = 0; return list.Where(x => int.TryParse(x, out ret)).Select(x => ret); };

    Benchmark
        .For(1000)
            .Execute("Int32Converter", () => func1(strings).Iterate())
            .Execute("int.TryParse", () => func2(strings).Iterate())
        .Report(4).Dump();
}

The int.TryParse is about 165 times faster than Int32Converter.

[Update 2]

The TypeConverter.IsValid inherited by the Int32Converter class contains a bug fixed in .NET 4.0.

Upvotes: 3

JohnB
JohnB

Reputation: 13723

I would define my own extension method as follows:

public static IEnumerable<int> AsIntegers (this IEnumerable<string> strings) {
   foreach (var s in strings) {
      int r; if (int.TryParse (s, r)) yield return r;
   }
}

...

List<int> intList = new List (stringList.AsIntegers ());

or

var intList = stringList.AsIntegers ().ToList ();

Upvotes: 1

Rune FS
Rune FS

Reputation: 21752

you can do that with a simple linq

int num = 0;
var ints = (from str in strings
            where int.TryParse(str,out num)
            select num).ToList();

Upvotes: 3

adrianm
adrianm

Reputation: 14726

I got some helper method like this

public static class CollectionUtilities {
    public static IEnumerable<int> ParseInt32(this IEnumerable<string> source) {
        return ParseInt32(source, NumberStyles.Integer, null);
    }

    public static IEnumerable<int?> TryParseInt32(this IEnumerable<string> source) {
        return TryParseInt32(source, NumberStyles.Integer, null);
    }

    public static IEnumerable<int> ParseInt32(this IEnumerable<string> source, IFormatProvider provider) {
        return ParseInt32(source, NumberStyles.Integer, provider);
    }

    public static IEnumerable<int?> TryParseInt32(this IEnumerable<string> source, IFormatProvider provider) {
        return TryParseInt32(source, NumberStyles.Integer, provider);
    }

    public static IEnumerable<int> ParseInt32(this IEnumerable<string> source, NumberStyles style) {
        return ParseInt32(source, style, null);
    }

    public static IEnumerable<int?> TryParseInt32(this IEnumerable<string> source, NumberStyles style) {
        return TryParseInt32(source, style, null);
    }

    public static IEnumerable<int> ParseInt32(this IEnumerable<string> source, NumberStyles style, IFormatProvider provider) {
        if (source == null)
            throw new ArgumentNullException("source");

        return ParseInt32Iterator(source, style, provider);
    }

    public static IEnumerable<int?> TryParseInt32(this IEnumerable<string> source, NumberStyles style, IFormatProvider provider) {
        if (source == null)
            throw new ArgumentNullException("source");

        return TryParseInt32Iterator(source, style, provider);
    }

    private static IEnumerable<int> ParseInt32Iterator(this IEnumerable<string> source, NumberStyles style, IFormatProvider provider) {
        foreach (string element in source) {
            yield return int.Parse(element, style, provider);
        }
    }

    private static IEnumerable<int?> TryParseInt32Iterator(this IEnumerable<string> source, NumberStyles style, IFormatProvider provider) {
        foreach (string element in source) {
            int value;
            bool success = int.TryParse(element, style, provider, out value);
            yield return success ? (int?)value : null;
        }
    }
}

With these helpers you code would look like:

var ints = strings.TryParseInt32().Where(x => x != null).Select(x => x.Value).ToList();

Upvotes: 1

usr
usr

Reputation: 171206

Just like that:

from s in strings
let parsed = s.TryParseInt32()
where parsed != null
select parsed.Value;

public static int? TryParseInt32(this string str)
{
    int i;
    if (!int.TryParse(str, NumberStyles.Integer, CultureInfo.InvariantCulture, out i))
        return null;
    return i;
}

The TryParseInt32 extension is reusable.

Here is an alternative:

from s in strings
select s.TryParseInt32() into parsed
where parsed != null
select parsed.Value;

Upvotes: 1

Related Questions