Share via


How to extract all numbers from string using c#?

Question

Wednesday, April 5, 2017 7:06 AM

Hi

I have some string vary in length and want to extract all numbers. please help.

Some of strings contain Arabic alphabet.

example:  az10long125654lut3545t75

desired output :

a=10

b=1255654

c=3545

d=75

Thank you in advance 

All replies (4)

Wednesday, April 5, 2017 7:16 AM

 private void ExtractIntegers()
        {
            string text = " az10long125654lut3545t75";
            string integerString = string.Empty;
            foreach(Char c in text)
            {
                if (Char.IsDigit(c))
                {
                    integerString += c;
                }
                else if (integerString.Length >0)
                {
                    Console.WriteLine(integerString);
                    integerString = "";
                }
            }  
        }

You can assign it to a variable in else block

Mark Answered, if it solves your question and Vote if you found it helpful.
Rohit Arora


Wednesday, April 5, 2017 8:21 AM

string input = "az10long125654lut3545t75";
            string[] inputSplit = input.ToLower.Split(new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'o', 'p', 'q','r', 's', 't', 'w', 'x', 'y', 'z' }, StringSplitOptions.RemoveEmptyEntries);

Split and remove letters, leaving the numbers in inputSplit array


Wednesday, April 5, 2017 8:26 AM

Hi,

You can always achieve this using regular expressions which is always a good technology to have in the toolbox:

using System;
using System.Text.RegularExpressions;

namespace MatchNumbers
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            var sampleString = "az10long125654lut3545t75";

            var pattern = "\\d";
            var matches = Regex.Matches(sampleString, pattern);
            Console.WriteLine($"Input string: {sampleString}");
            foreach (Match m in matches)
                Console.WriteLine($"Matched: {m}");

            Console.ReadLine();
        }
    }
}

Regards,

D

If you find this helpful please either vote it or mark it as the answer


Thursday, April 6, 2017 2:06 AM

For the sake of completeness, here's the old school way. (Also explains why RohitArora's reply is not an answer)

            string input = "az10long125654lut3545t75";
            List<string> output = new List<string>();
            StringBuilder buffer = new StringBuilder();
            foreach (char c in input)
            {
                if (Char.IsDigit(c))
                {
                    buffer.Append(c);
                }
                else
                {
                    if (buffer.Length > 0)
                    {
                        output.Add(buffer.ToString());
                        buffer.Length = 0;
                    }
                }
            }
            if (buffer.Length > 0)  // if buffer is not empty, appending to the output list
            {
                output.Add(buffer.ToString());
            }
            foreach (string item in output)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();