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
:afrog:
(Using .net cf 2.0)
Printable View
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
:afrog:
(Using .net cf 2.0)
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!!
I've never heard of that...is that available with the .net compact framework 2.0 (which is the version i've specified)?
Mid and IsNumeric are both VB6 stowaways.VB Code:
If Char.IsDigit(myString(0)) Then 'The string starts with a number. End IfVB 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
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..!
:p Well i learned something new today too. I will start using both of those meshelf :afrog:
happy coding!!
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?
I believe tryparse isn't supported by the compact framework. Am i right in saying this? If so then what alternative can i use?
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 :)
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
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
Regex is in the compact framework..
Quote:
Originally Posted by MSDN
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! :thumb:
:wave:
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"
You can also be sooooo much cooler if you use Regex... because ... its... like.. cool.. and stuff :DVB Code:
If System.Text.RegularExpressions.Regex.IsMatch(str, "^[\d].*$") Then 'it starts with a digit.... End If
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
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?)
:thumb: