# VBForums CodeBank > CodeBank - C# >  Extract Numbers From String

## Sir Loin

Here is a method that will extract all of the numbers from a string.



```

        private string ExtractNumbers(string Expression)
        {
            string result = null;
            char Letter;

            for (int i = 0; i < Expression.Length; i++)
            {
                Letter = Convert.ToChar(Expression.Substring(i, 1));

                if (Char.IsNumber(Letter))
                {
                    result += Letter.ToString();
                }
            }

            // MessageBox.Show(result);
            return result;
        } 



```

It works well.

-Sir Loin

----------


## Kasracer

Why are you extracting numbers and then putting them into a string? It would seem to make more sense to return an array of integers.

Also, instead of converting each substring to a character, it would be simplier to just access the string like an array for the character (MyString[i] = 'c')

----------


## wild_bill

Don't forget the power of regex:


```
        static string ExtractNumbers(string Expression)
        {
              return string.Join(null,System.Text.RegularExpressions.Regex.Split(Expression, "[^\\d]"));
        }
```

----------


## THEROB

I use this - it seems to be faster,  however, same as the others, it only works for positive integers:


```
        private int GetNumericValue(string sVal)
        {
            int iFirst, iCharVal, iEnd;
            int iMult = 1, iRet = 0;
            char[] aNumbers = "1234567890".ToCharArray();

            iFirst = sVal.IndexOfAny(aNumbers);
            iEnd = sVal.LastIndexOfAny(aNumbers);
            if (iEnd < 0)
                return 0;
            string subStr = sVal.Substring(iFirst, iEnd - iFirst + 1);
            iEnd = subStr.Length - 1;
            while (subStr.Length > 0)
            {
                iCharVal = int.Parse(subStr[subStr.Length-1].ToString());
                iRet += iMult * iCharVal;
                iMult *= 10;

                if (iEnd <= 0)
                    break;
                subStr = subStr.Substring(0, subStr.Length - 1);
                iEnd = subStr.LastIndexOfAny(aNumbers);
                subStr = sVal.Substring(iFirst, iEnd + 1);
                
            }
            return iRet;
        }
```

----------


## THEROB

This one will return a doube


```
        private double GetNumericValue(string sVal)
        {
            int iFirst, iEnd;
            char[] aNumbers = "1234567890.-".ToCharArray();
            string sRet = null;
            bool bMinus = false, bPoint=false;

            iFirst = sVal.IndexOfAny(aNumbers);
            iEnd = sVal.LastIndexOfAny(aNumbers);
            if (iEnd < 0)
                return 0;

            string subStr = sVal.Substring(iFirst, iEnd - iFirst + 1);
            iEnd = subStr.Length - 1;
            char cMinus = Convert.ToChar("-");
            char cPoint = Convert.ToChar(".");
            while (subStr.Length > 0)
            {
                if (subStr[0].Equals(cMinus))
                    bMinus = !bMinus;
                else if (subStr[0].Equals(cPoint))
                {
                    if (!bPoint)
                    {
                        bPoint = true;
                        sRet += subStr[0];
                    }
                }
                else
                    sRet += subStr[0];

                subStr = subStr.Substring(1, subStr.Length-1);
                if (subStr.Length <= 0)
                    break;
                iFirst = subStr.IndexOfAny(aNumbers);
                subStr = subStr.Substring(iFirst, subStr.Length - iFirst);
            }

            if (bMinus)
                sRet = "-" + sRet;
            return double.Parse(sRet);
        }
```

----------


## danasegarane

Here is another Method Using Regular Expressions

vb Code:
//using System.Text.RegularExpressions 'global Declartion
            Regex Strings =new Regex("[0-9]");
            string sString = "0130.1Test";
            string sExtracted = Strings.Replace(sString, "");
            MessageBox.Show(sExtracted);

----------

