|
-
Mar 18th, 2007, 03:45 PM
#1
Thread Starter
Member
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
-
Mar 18th, 2007, 03:50 PM
#2
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
-
Mar 18th, 2007, 05:05 PM
#3
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
 
-
Mar 18th, 2007, 08:03 PM
#4
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.
-
Mar 19th, 2007, 11:22 AM
#5
Thread Starter
Member
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
-
Mar 19th, 2007, 11:26 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|