Results 1 to 6 of 6

Thread: Extract Numbers From String

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Extract Numbers From String

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

    PHP Code:
            private string ExtractNumbers(string Expression)
            {
                
    string result null;
                
    char Letter;

                for (
    int i 0Expression.Lengthi++)
                {
                    
    Letter Convert.ToChar(Expression.Substring(i1));

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

                
    // MessageBox.Show(result);
                
    return result;
            } 
    It works well.

    -Sir Loin

  2. #2
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Extract Numbers From String

    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')
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  3. #3
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Extract Numbers From String

    Don't forget the power of regex:
    Code:
            static string ExtractNumbers(string Expression)
            {
                  return string.Join(null,System.Text.RegularExpressions.Regex.Split(Expression, "[^\\d]"));
            }

  4. #4
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575

    Re: Extract Numbers From String

    I use this - it seems to be faster, however, same as the others, it only works for positive integers:
    Code:
            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;
            }
    Last edited by THEROB; Feb 9th, 2007 at 04:16 AM.
    My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]

  5. #5
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575

    Re: Extract Numbers From String

    This one will return a doube
    Code:
            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);
            }
    My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]

  6. #6
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Extract Numbers From String

    Here is another Method Using Regular Expressions
    vb Code:
    1. //using System.Text.RegularExpressions 'global Declartion
    2.             Regex Strings =new Regex("[0-9]");
    3.             string sString = "0130.1Test";
    4.             string sExtracted = Strings.Replace(sString, "");
    5.             MessageBox.Show(sExtracted);
    Please mark you thread resolved using the Thread Tools as shown

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width