Results 1 to 16 of 16

Thread: [RESOLVED] Check whether a string starts with a number

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Resolved [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

  2. #2
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    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!!

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    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

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

    Re: Check whether a string starts with a number

    Mid and IsNumeric are both VB6 stowaways.
    VB Code:
    1. If Char.IsDigit(myString(0)) Then
    2.     'The string starts with a number.
    3. End If
    VB Code:
    1. Dim number As Integer
    2.  
    3. If Integer.TryParse(myString.Substring(length), number Then
    4.     'The first 'length' characters of the string represent a number.
    5. End If
    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
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. If Integer.TryParse(String1.SubString(0,1)) Then
    2.    'First character is a number!
    3. End If

    Edit: Dang it! ..defeated again..!
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    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!!

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    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

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    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

  9. #9
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    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

  10. #10
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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:
    1. 'the function
    2. Private Function StartsDigit(ByVal str As String) As Boolean
    3.         If System.Text.RegularExpressions.Regex.IsMatch(str, "^[\d].*$") Then
    4.             Return True
    5.         Else
    6.             Return False
    7.         End If
    8. End Function
    9.  
    10. 'usage
    11. MessageBox.Show(StartsDigit("4test").ToString) 'returns True
    12. MessageBox.Show(StartsDigit("test").ToString) 'returns False
    Last edited by gigemboy; Dec 15th, 2006 at 09:51 AM. Reason: Modified Regex Pattern

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    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

  12. #12
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Check whether a string starts with a number

    Regex is in the compact framework..
    Quote 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.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    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

  14. #14
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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:
    1. If System.Text.RegularExpressions.Regex.IsMatch(str, "^[\d].*$") Then
    2.      'it starts with a digit....
    3. 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.

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

    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:
    1. Dim number As Integer
    2.  
    3. If Integer.TryParse(myString.Substring(0, substringLength), _
    4.                     Globalization.NumberStyles.None, _
    5.                     Nothing, _
    6.                     number) Then
    7.     'The string starts with a number of at least substringLength digits.
    8. End If
    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

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    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
  •  



Click Here to Expand Forum to Full Width