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 + " ");

Next Post Previous Post
  • Anonymous
    Anonymous March 30, 2023 at 10:10 PM

    Very good logic

Add Comment
comment url