<<  May 2012  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Extracting numbers from a string in C#

by Tim 14. July 2010 11:06

Lately, I ran into the old gem of extracting numbers from a string. I needed to do this because I couldn't get the number back from the database table but I could see the number in a the product title. Here is a code snippet in C# (using regular expressions) that works quite nicely. Any number of tests can be made on the resulting string array.

        string[] sSplitLine;
        string sResult = string.Empty;
        string sMsg = "Eiger 1214 post number count 14";

        sSplitLine = Regex.Split(sMsg, "[^\\d]");
        foreach (string sChar in sSplitLine)
        {
            if (sChar != string.Empty)
            {
                sResult += sChar + "
"; } } lblMessage.Text = sResult;

The results when displayed:

1214
14

Comments are closed