|
-
Dec 15th, 2006, 07:40 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Check whether a string starts with a number
Hi guys,
How do i check whether a string starts with a number (various digits long)?
I just need it to return true if it does and false if it doesn't.
Thanks in advance for any help

(Using .net cf 2.0)
If your problem has been solved then please mark the thread [RESOLVED].
If i have helped then please Rate my post 
-
Dec 15th, 2006, 08:11 AM
#2
Fanatic Member
Re: Check whether a string starts with a number
I would suggest using Mid$(). You can get to whatever part of the string is needed to test on and you can then test just that portion. For example
i = Mid$(Text,0,1)
If IsNumeric(i) then
'Do Something for true
Else
'Do Something for false
End If
Hope this helps!!
-
Dec 15th, 2006, 08:41 AM
#3
Thread Starter
Fanatic Member
Re: Check whether a string starts with a number
I've never heard of that...is that available with the .net compact framework 2.0 (which is the version i've specified)?
If your problem has been solved then please mark the thread [RESOLVED].
If i have helped then please Rate my post 
-
Dec 15th, 2006, 08:41 AM
#4
Re: Check whether a string starts with a number
Mid and IsNumeric are both VB6 stowaways.
VB Code:
If Char.IsDigit(myString(0)) Then
'The string starts with a number.
End If
VB Code:
Dim number As Integer
If Integer.TryParse(myString.Substring(length), number Then
'The first 'length' characters of the string represent a number.
End If
-
Dec 15th, 2006, 08:44 AM
#5
Re: Check whether a string starts with a number
I wouldnt suggest Mid() Its an old VB6 function (and so is IsNumeric), you should get familiar with the SubString and TryParse method.
VB Code:
If Integer.TryParse(String1.SubString(0,1)) Then
'First character is a number!
End If
Edit: Dang it! ..defeated again..!
-
Dec 15th, 2006, 08:53 AM
#6
Fanatic Member
Re: Check whether a string starts with a number
Well i learned something new today too. I will start using both of those meshelf
happy coding!!
-
Dec 15th, 2006, 09:02 AM
#7
Thread Starter
Fanatic Member
Re: Check whether a string starts with a number
I have a few problems:
i'm getting an error saying TryParse is not a member of Integer (do i need to add any references?)
And also i'm getting another error saying: The targeted version of the .NET Compact Framework does not support latebinding.
Any reason why i would be getting them?
If your problem has been solved then please mark the thread [RESOLVED].
If i have helped then please Rate my post 
-
Dec 15th, 2006, 09:08 AM
#8
Thread Starter
Fanatic Member
Re: Check whether a string starts with a number
I believe tryparse isn't supported by the compact framework. Am i right in saying this? If so then what alternative can i use?
If your problem has been solved then please mark the thread [RESOLVED].
If i have helped then please Rate my post 
-
Dec 15th, 2006, 09:24 AM
#9
Re: Check whether a string starts with a number
You are right. IsNumeric will do the job until someone comes up with a better idea(if at all necessary). If you don't specify you are using the CF you get mildly flamed. If you do specify that, you can still get not applicable suggestions but without the flaming part.
--------
EDIT: Char.IsDigit is also supported, so jmcilhinney saves the day once again
Last edited by Half; Dec 15th, 2006 at 09:28 AM.
VB 2005, Win Xp Pro sp2
-
Dec 15th, 2006, 09:44 AM
#10
Re: Check whether a string starts with a number
Don't forget about Regex (I love bringing it up)
**EDITED - put "^" and "$" in pattern
VB Code:
'the function
Private Function StartsDigit(ByVal str As String) As Boolean
If System.Text.RegularExpressions.Regex.IsMatch(str, "^[\d].*$") Then
Return True
Else
Return False
End If
End Function
'usage
MessageBox.Show(StartsDigit("4test").ToString) 'returns True
MessageBox.Show(StartsDigit("test").ToString) 'returns False
Last edited by gigemboy; Dec 15th, 2006 at 09:51 AM.
Reason: Modified Regex Pattern
-
Dec 15th, 2006, 10:10 AM
#11
Thread Starter
Fanatic Member
Re: Check whether a string starts with a number
Thanks for the help....i'd just like to note tho that i have supplied the fact that i am using cf twice in previous posts!
But i'll try jmcilhinney's first option and see if that works
If your problem has been solved then please mark the thread [RESOLVED].
If i have helped then please Rate my post 
-
Dec 15th, 2006, 10:12 AM
#12
Re: Check whether a string starts with a number
Regex is in the compact framework..
 Originally Posted by MSDN
Regex.IsMatch (String)
Indicates whether the regular expression specified in the Regex constructor finds a match in the input string.
Supported by the .NET Compact Framework.
Last edited by gigemboy; Dec 15th, 2006 at 10:16 AM.
-
Dec 15th, 2006, 10:46 AM
#13
Thread Starter
Fanatic Member
Re: Check whether a string starts with a number
Thanks for the suggestion but i just prefer to keep things short and sweet so the shorter the code the better in my opinion! Thanks again for all the help though from everyone! Problem solved! 
If your problem has been solved then please mark the thread [RESOLVED].
If i have helped then please Rate my post 
-
Dec 15th, 2006, 10:54 AM
#14
Re: [RESOLVED] Check whether a string starts with a number
Well mine can be short and sweet too, but you asked for something that "returned true or false" which I actually took that too literally and did in the function...
The "short and sweet version"
VB Code:
If System.Text.RegularExpressions.Regex.IsMatch(str, "^[\d].*$") Then
'it starts with a digit....
End If
You can also be sooooo much cooler if you use Regex... because ... its... like.. cool.. and stuff
Last edited by gigemboy; Dec 15th, 2006 at 10:59 AM.
-
Dec 15th, 2006, 07:23 PM
#15
Re: [RESOLVED] Check whether a string starts with a number
Note that Integer.TryParse is supported on the CF, but not the overload that I suggested. My apologies for that. My code should have read:
VB Code:
Dim number As Integer
If Integer.TryParse(myString.Substring(0, substringLength), _
Globalization.NumberStyles.None, _
Nothing, _
number) Then
'The string starts with a number of at least substringLength digits.
End If
-
Dec 18th, 2006, 05:32 AM
#16
Thread Starter
Fanatic Member
Re: [RESOLVED] Check whether a string starts with a number
Ok well thanks for the amendment. I'll remember that for next time, but you're other suggestion works perfectly fine for now. (Unless you suggest using that one instead?)
If your problem has been solved then please mark the thread [RESOLVED].
If i have helped then please Rate my post 
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
|