locked
detecting numeric RRS feed

  • Question

  • hai all, how can we detect a numeric and string? but i don't want to use isnumeric and a keypress..
    without both of them can we detecting, which one is string or numeric...
    Friday, May 29, 2009 1:58 AM

Answers

  • A string is a string is a string...
    Anything you input into a TextBox is a string, whether it includes numbers or not.
    If you don't want to use IsNumeric and KeyPress (by the way, any specific reason you don't want to use them?), you can try Double.TryParse. If it returns True, then it is a double (number); if it returns False, it is not a double (hence a string).
    Try this code. If only numbers in the TextBox, it will display "double" in the Label. If the text is only letters or a mix of letters and numbers, it will display "string"



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim mynumber As Double
            If Double.TryParse(TextBox1.Text, mynumber) Then
                Label1.Text = "double"
            Else
                Label1.Text = "string"
            End If
        End Sub
    Friday, May 29, 2009 5:21 AM
  • Suppose You have a string

     Dim InputValue As String = "mis1122cbncb456"

    You want to find whether it has numbers then you can user regular expressions.

            Dim matches = System.Text.RegularExpressions.Regex.Matches(InputValue, "\d+")
            If matches.Count > 0 Then
                MessageBox.Show("Number Found")
               

                'If you want to display all found number

                For Each m As System.Text.RegularExpressions.Match In matches
                    MessageBox.Show(m.Value.ToString)
                Next

            End If

    Arjun Paudel
    Saturday, May 30, 2009 4:27 AM

All replies

  • Well, you could do this:

    aString.GetType.ToString
    anInt.GeyType.ToString

    aString will return "System.String"
    anInt will return "System.Int32"

    You can do this with an object, too.

    anObject = "My String"
    anObject.GetType.ToString will return "System.String"

    For your case you could try it an a string, if it's not a string, it must be an int, long, etc.
    Friday, May 29, 2009 2:37 AM
  • if i input me123 which one the above code will detect..i think it will detect string and numeric...
    Friday, May 29, 2009 2:55 AM
  • Oh, I'm terribly sorry. I didn't notice you said string and numeric. Thought you said 'or.'
    Friday, May 29, 2009 3:18 AM
  • A string is a string is a string...
    Anything you input into a TextBox is a string, whether it includes numbers or not.
    If you don't want to use IsNumeric and KeyPress (by the way, any specific reason you don't want to use them?), you can try Double.TryParse. If it returns True, then it is a double (number); if it returns False, it is not a double (hence a string).
    Try this code. If only numbers in the TextBox, it will display "double" in the Label. If the text is only letters or a mix of letters and numbers, it will display "string"



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim mynumber As Double
            If Double.TryParse(TextBox1.Text, mynumber) Then
                Label1.Text = "double"
            Else
                Label1.Text = "string"
            End If
        End Sub
    Friday, May 29, 2009 5:21 AM
  • if i input me123 which one the above code will detect..i think it will detect string and numeric...

    Your question is really just a bit too vague to provide an exact answer 
    That is why your question was initially misunderstood. 

    You should provide more context and background.
    Provide examples of input strings and what the results should be
    Provide some code example of what you have so far, noting any errors you might get.
    ETC.
    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, May 29, 2009 12:56 PM
  • cory for make you misunderstood..my english is rather missy..
    haha...cory for all.tq..i hope can learn visual studio with you.tq a lot..
    y..i know that every input in the textbox re usually string..haha..i forgot a simple matter...cory..
    Saturday, May 30, 2009 4:01 AM
  • Suppose You have a string

     Dim InputValue As String = "mis1122cbncb456"

    You want to find whether it has numbers then you can user regular expressions.

            Dim matches = System.Text.RegularExpressions.Regex.Matches(InputValue, "\d+")
            If matches.Count > 0 Then
                MessageBox.Show("Number Found")
               

                'If you want to display all found number

                For Each m As System.Text.RegularExpressions.Match In matches
                    MessageBox.Show(m.Value.ToString)
                Next

            End If

    Arjun Paudel
    Saturday, May 30, 2009 4:27 AM