B4J Code Snippet IsUnsignedInteger/IsSignedInteger/IsUnsignedDecimal/IsSignedDecimal/GetDecimalSeparator

I use these in B4J to check if a string matches the respective conditions; I guess it will work in B4A and B4I as well. If there are easier ways to do this (I didn't find any) or you'd see something fishy, holler! And enjoy, hope this is useful :)

EDIT: hiccup corrected in the ...Decimal subs - d'oh...

B4X:
Sub IsUnsignedInteger(s As String, allowNullString As Boolean) As Boolean

    ' Regex expression based on http://regexlib.com/Search.aspx?k=integer&AspxAutoDetectCookieSupport=1
    If allowNullString And (s = "") Then
        Return True
    Else
        Return IsNumber(s) And Regex.IsMatch("^\d+$", s)
    End If

End Sub

Sub IsUnsignedDecimal(s As String, allowNullString As Boolean, decSep As String) As Boolean

    Dim sep As String
    If decSep.Trim = "" Then
        sep = GetDecimalSeparator
    Else
        sep = decSep
    End If

    ' Regex expression based on https://community.oracle.com/thread/429716
    If allowNullString And (s = "") Then
        Return True
    Else
        Return IsNumber(s) And Regex.IsMatch("^(\" & sep & "[0-9]+|[0-9]+(\" & sep & "[0-9]*)*)$", s)
    End If

End Sub

Sub IsSignedInteger(s As String, allowNullString As Boolean) As Boolean

    ' Regex expression based on http://regexlib.com/Search.aspx?k=integer&AspxAutoDetectCookieSupport=1
    If allowNullString And (s = "") Then
        Return True
    Else
        Return IsNumber(s) And Regex.IsMatch("^(\+|-)?\d+$", s)
    End If

End Sub

Sub IsSignedDecimal(s As String, allowNullString As Boolean, decSep As String) As Boolean

    Dim sep As String
    If decSep.Trim = "" Then
        sep = GetDecimalSeparator
    Else
        sep = decSep
    End If

    ' Regex expression based on https://community.oracle.com/thread/429716
    ' combined with http://regexlib.com/Search.aspx?k=integer&AspxAutoDetectCookieSupport=1
    If allowNullString And (s = "") Then
        Return True
    Else
        Return IsNumber(s) And Regex.IsMatch("^(\+|-)?(\" & sep & "[0-9]+|[0-9]+(\" & sep & "[0-9]*)*)$", s)
    End If

End Sub

Sub GetDecimalSeparator As String

    Return NumberFormat2(1.1, 1, 1, 1, False).SubString2(1, 2)

End Sub
 
Last edited:
Top