Results 1 to 6 of 6

Thread: extracting numbers

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    58

    extracting numbers

    Hi, I have a text file which contains an 11 digit number, I've gotten my program to read the file and it all seems fine (it can display the number) . I also want to display and store (as an int) the last four digits of the 11 digit number, but I have no idea how, so any help whould be great?

    thanks,
    J

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: extracting numbers

    Did you look at the substring function of strings? You could use that and Integer.TryParse to get the integer value you want (substring the whole from 8th position to the last).
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: extracting numbers

    That would be the best way, as long as you are using 2005. TryParse is not available prior to 2005, which would leave you with two options. First, you might just assume that the number is valid, which may be safe, since you created it. If you feel that the assumption is safe, you could convert it using CInt(). Alternatively, you might use Val(), which will not raise an exception if the value isn't an integer. Either way, you want to use SubString() to get those last characters.
    My usual boring signature: Nothing

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: extracting numbers

    Actually, in 2003 Double.TryParse allows you to parse a string looking for just integer values. You just have to specify the appropriate NumberStyles value. The result will be a Double, which you can then safely convert to an Integer.

    Of course, if you are using 2005 then Integer.TryParse would be the way to go. Please use the radio buttons provided to specify your version when creating threads in future.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    58

    Re: extracting numbers

    Thanks for your help. I've looked up subString and It was just what I was looking for. If your interested here's what I did...

    Code:
    myString = myString.Substring(myString.Length - 4)
            myInt = Convert.ToInt32(myString)
            txtDisplay.Text = myString
    so if the number is 00000001234, I can pull out the 1234. Thanks

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: extracting numbers

    I believe Convert.ToInt will raise an exception if the string in question is not a valid number. Integer.TryParse would be a better solution in 2005.
    My usual boring signature: Nothing

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