More Group Sites
Education Books
School Rankings
Jobless Net
Better Home
Enviro++
更好教育论坛


Help | Subscribe/Unsubscribe | Rules | Other Group Sites: Better Education | Better Education Forum
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Share
Options
View
Go to last post Go to first unread
hong  
#1 Posted : Sunday, April 12, 2009 3:36:50 AM(UTC)
hong

Rank: Administration

Reputation:

Groups: AcademicCoachingSchool, admin, Administration, BookSeller, CatholicSchool, CoachingAdult, CoachingProfessional, CoachingSports, ExtraCurriculumCoaching, IndependentSchool, Moderator, MusicTeacher, PrivateSchool, PublicSchool, SelectiveSchool, tutor
Joined: 11/23/2008(UTC)
Posts: 521

Extract numbers from a string


There are a few ways to extract numbers from a string:
1. Extract an integer from a string
Code:
static int ExtractIntFromString(string strInput) 
{ 
    System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(); 
    System.Text.StringBuilder stringBuilderInt = new System.Text.StringBuilder(); 
 
    //Make the character array using the ToCharArray() method. 
    for (int i = 0 ; i < strInput.Length; i++) 
    { 
        if (Char.IsDigit(strInput[i])) 
            stringBuilderInt.Append(strInput[i]); 
        else 
            stringBuilder.Append(strInput[i]); 
    } 

    int nResult = Int32.Parse(stringBuilderInt.ToString()); 
    
    return nResult; 
} 

2. Another way to extract an integer from a string
Code:
static int ExtractNumbers(string StrInput) 
        { 
            string strResult = null; 
            char ch; 
 
            for (int i = 0; i < StrInput.Length; i++) 
            { 
                ch = Convert.ToChar(StrInput[i]); 
 
                if (Char.IsNumber(ch)) 
                { 
                    strResult += ch.ToString(); 
                } 
            } 
 
             int nResult = Int32.Parse(stringBuilderInt.ToString()); 
    
             return nResult; 
        } 

3. Using Regular Expression
Code:
        static string ExtractNumbers(string StrInput)
        {
              return string.Join(null,System.Text.Regular Expression.Regex.Split(StrInput, "[^\\d]"));
        }


4. Extract a positive integer from a string:
Code:
        private int ExtractPositiveIntegerFromString(string strVal)
        {
            int nFirst, nCharVal, nEnd;
            int nMult = 1, nRet = 0;
            char[] aNumbers = "1234567890".ToCharArray();
 
            nFirst = strVal.IndexOfAny(aNumbers);
            nEnd = strVal.LastIndexOfAny(aNumbers);
            if (nEnd < 0)
                return 0;
            string subStr = strVal.Substring(nFirst, nEnd - nFirst + 1);
            nEnd = subStr.Length - 1;
            while (subStr.Length > 0)
            {
                nCharVal = int.Parse(subStr[subStr.Length-1].ToString());
                nRet += nMult * nCharVal;
                nMult *= 10;
 
                if (nEnd <= 0)
                    break;
                subStr = subStr.Substring(0, subStr.Length - 1);
                nEnd = subStr.LastIndexOfAny(aNumbers);
                subStr = strVal.Substring(nFirst, nEnd + 1);
                
            }
            return nRet;
        }


5. Extract a double from a string
Code:
        private double ExtractDoubleFromString(string strVal)
        {
            int nFirst, nEnd;
            char[] aNumbers = "1234567890.-".ToCharArray();
            string strResult = null;
            bool bMinus = false, bPoint=false;
 
            nFirst = strVal.IndexOfAny(aNumbers);
            nEnd = strVal.LastIndexOfAny(aNumbers);
            if (nEnd < 0)
                return 0;
 
            string subStr = strVal.Substring(nFirst, nEnd - nFirst + 1);
            nEnd = 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;
                        strResult += subStr[0];
                    }
                }
                else
                    strResult += subStr[0];
 
                subStr = subStr.Substring(1, subStr.Length-1);
                if (subStr.Length <= 0)
                    break;
                nFirst = subStr.IndexOfAny(aNumbers);
                subStr = subStr.Substring(nFirst, subStr.Length - nFirst);
            }
 
            if (bMinus)
                strResult = "-" + strResult;
            return double.Parse(strResult);
        } 











Sponsor
Rss Feed  Atom Feed
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.