none
check string if numric RRS feed

  • Question

  • Welcome!

    1- I used the following code but did not take into account the decimal point
    For example 12.5 consider it non-numeric

      If Not Integer.TryParse("12.55", Nothing) Then
                MsgBox("not numric") : Exit Sub
            End If

    2- I want to delete the arithmetic transactions from the first section or the last section only, for example

    12++

    Become 12

    */1155++

    Become 1155

    Monday, August 6, 2018 10:06 PM

Answers

  • Hi

     

    1.  You used Integer.TryParse and 12.55 is not an Integer

         One alternative:

        Dim s As String = "12.55"
        Dim Numeric As Boolean = IsNumeric(s)
        If Not Numeric Then
          MessageBox.Show("String is NOT Numeric")
        End If

    2.

        Dim s As String = "*/1155++"
        Dim s2 As String = s.Replace("*", Nothing).Replace("/", Nothing).Replace("+", Nothing)
        MessageBox.Show(s2)




    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, August 6, 2018 11:56 PM
    • Marked as answer by ahmeddc Tuesday, August 7, 2018 4:39 AM
    Monday, August 6, 2018 11:12 PM
  • Here is an alternative.

    Public Module ExtensionMethods
        <Runtime.CompilerServices.Extension>
        Public Function ReplaceSpecial(sender As String) As String
            Dim separators() As Char = {"/"c, "+"c, "-"c, "="c, "*"c, "."c}
            Return String.Join("", sender.Split(separators, StringSplitOptions.RemoveEmptyEntries))
        End Function
    End Module

    Usage

    Dim value As String = "*/11.55++"
    Dim result = value.ReplaceSpecial()
    Dim intValue As Integer = 0
    If Integer.TryParse(result, intValue) Then
        Console.WriteLine(intValue)
    Else
        Console.WriteLine($"{result} is not valid")
    End If


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by ahmeddc Tuesday, August 7, 2018 4:39 AM
    Tuesday, August 7, 2018 12:31 AM
    Moderator
  • Hi

    You can try regular expressions.

    1.

    Public Function isNumberic(ByVal message As String) As Boolean Dim rex As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("^(-?[0-9]+[.]?[0-9]*)$") If rex.IsMatch(message) Then Return True Else Return False End If End Function

    2.

    Public Function RemoveNotNumber(ByVal key As String) As String Return System.Text.RegularExpressions.Regex.Replace(key, "[^\d.\d]", "")

    End Function

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 7, 2018 2:25 AM
  • Regex is a good way to handle this, particularly if the examples you gave are just a couple possible examples and there could be many other kinds of strings you want to convert.

    Regex does have a certain amount of overhead in .Net though, and a character-by-character parsing could be faster.  Here's an example of one such routine.  You could modify this to handle other possible numeric characters, both in the character check (e.g. include comma or currency) and in the conversion (e.g. add other allowed number styles in TryParse).  It depends on what kinds of text you want to allow.

    Private Function ParseNumberFromString(text As String) As Double?
        Dim validChars As New Text.StringBuilder
        For Each c In text
            If Char.IsNumber(c) OrElse c = "."c OrElse c = "-"c Then
                validChars.Append(c)
            End If
        Next
        Dim result As Double
        If Double.TryParse(validChars.ToString, result) Then
            Return result
        End If
        Return Nothing
    End Function
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by ahmeddc Tuesday, August 7, 2018 4:39 AM
    Tuesday, August 7, 2018 2:44 AM
    Moderator

All replies

  • Hi

     

    1.  You used Integer.TryParse and 12.55 is not an Integer

         One alternative:

        Dim s As String = "12.55"
        Dim Numeric As Boolean = IsNumeric(s)
        If Not Numeric Then
          MessageBox.Show("String is NOT Numeric")
        End If

    2.

        Dim s As String = "*/1155++"
        Dim s2 As String = s.Replace("*", Nothing).Replace("/", Nothing).Replace("+", Nothing)
        MessageBox.Show(s2)




    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, August 6, 2018 11:56 PM
    • Marked as answer by ahmeddc Tuesday, August 7, 2018 4:39 AM
    Monday, August 6, 2018 11:12 PM
  • Here is an alternative.

    Public Module ExtensionMethods
        <Runtime.CompilerServices.Extension>
        Public Function ReplaceSpecial(sender As String) As String
            Dim separators() As Char = {"/"c, "+"c, "-"c, "="c, "*"c, "."c}
            Return String.Join("", sender.Split(separators, StringSplitOptions.RemoveEmptyEntries))
        End Function
    End Module

    Usage

    Dim value As String = "*/11.55++"
    Dim result = value.ReplaceSpecial()
    Dim intValue As Integer = 0
    If Integer.TryParse(result, intValue) Then
        Console.WriteLine(intValue)
    Else
        Console.WriteLine($"{result} is not valid")
    End If


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by ahmeddc Tuesday, August 7, 2018 4:39 AM
    Tuesday, August 7, 2018 12:31 AM
    Moderator
  • Hi

    You can try regular expressions.

    1.

    Public Function isNumberic(ByVal message As String) As Boolean Dim rex As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("^(-?[0-9]+[.]?[0-9]*)$") If rex.IsMatch(message) Then Return True Else Return False End If End Function

    2.

    Public Function RemoveNotNumber(ByVal key As String) As String Return System.Text.RegularExpressions.Regex.Replace(key, "[^\d.\d]", "")

    End Function

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 7, 2018 2:25 AM
  • Regex is a good way to handle this, particularly if the examples you gave are just a couple possible examples and there could be many other kinds of strings you want to convert.

    Regex does have a certain amount of overhead in .Net though, and a character-by-character parsing could be faster.  Here's an example of one such routine.  You could modify this to handle other possible numeric characters, both in the character check (e.g. include comma or currency) and in the conversion (e.g. add other allowed number styles in TryParse).  It depends on what kinds of text you want to allow.

    Private Function ParseNumberFromString(text As String) As Double?
        Dim validChars As New Text.StringBuilder
        For Each c In text
            If Char.IsNumber(c) OrElse c = "."c OrElse c = "-"c Then
                validChars.Append(c)
            End If
        Next
        Dim result As Double
        If Double.TryParse(validChars.ToString, result) Then
            Return result
        End If
        Return Nothing
    End Function
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by ahmeddc Tuesday, August 7, 2018 4:39 AM
    Tuesday, August 7, 2018 2:44 AM
    Moderator
  • thank you all
    Tuesday, August 7, 2018 4:40 AM