How to extract number from string using Linq

Imagine you have an string("ABCDE99F-J74-12-89A"), and you want to extract the only number from the string. This snippet will show how to extract numbers from strings.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Linq_Tips
{
    class Program
    {
        static void Main(string[] args)
        {
            string aString = "ABCDE99F-J74-12-89A";
            // Select only those characters that are numbers 
            IEnumerable<char> stringQuery = from ch in aString
                                            where Char.IsDigit(ch)
                                            select ch;
            // Execute the query 
            foreach (char c in stringQuery)
                Console.Write(c + " ");

        }
    }
}

1 Comments

Please do not post any spam link in the comment box😊

Post a Comment
Previous Post Next Post