B4J Code Snippet isLetters - check if string are letters

Discussion in 'B4J Code Snippets' started by Knoppi, Sep 4, 2017.

  1. Knoppi

    Knoppi Active Member Licensed User

    Don't do something like this:
    Code:
    'This code is WRONG!
    Sub IsLetter(L As StringAs Boolean
        
    return ( "ABCDEFGHIJKLMNOPQRSTUVWXYZ".Contains( L.ToUpperCase) )
    End Sub
    or this
    Code:
    'Visual Basic Code
    'This code is WRONG!
    Function IsLetter(strValue As StringAs Boolean
      
    Dim intPos As Integer  For intPos = 1 To Len(strValue)
        
    Select Case Asc(Mid(strValue, intPos, 1))
          
    Case 65 To 9097 To 122
            IsLetter = 
    True
       
    Case Else
         IsLetter = 
    False
         
    Exit For
       
    End Select
    Next
    End Function
    It works only with English and a few other languages.

    The isLetter method form java returns true if the character is a letter in Chinese, German, Arabic, or another language.
    https://docs.oracle.com/javase/tutorial/i18n/text/charintro.html

    Library: JavaObject

    Code:
    'check if string are letters
    Sub isLetters( Text As String ) As Boolean
        
    Dim ret As Boolean
        
    For i=0 To Text.Length -1
            ret = isLetter( Text.CharAt(i))
            
    If ret = False  Then Return False
        
    Next
        
    Return True
    End Sub

    'check if aChar is a letter
    Sub isLetter( aChar As Char) As Boolean
        
    Dim jo As JavaObject
        
    Return jo.InitializeStatic("java.lang.Character").RunMethod( "isLetter"Array( aChar))
    End Sub
    Example:
    Code:
    Dim s As String = "aüßáàâăąæåã"
    Log( isLetters( s))   'return true
     
    Kope, stingrae and jmon like this.
  2. Erel

    Erel Administrator Staff Member Licensed User

    An implementation that works in B4A, B4i and B4J:
    Code:
    Sub IsLetter (c As StringAs Boolean
       
    Dim pattern As String = "\p{Alpha}"
       
    If IsDevTool("B4J"Then pattern = "(?U)" & pattern
       
    Return Regex.IsMatch(pattern, c)
    End Sub
     
    stingrae, jmon and Knoppi like this.
  3. Knoppi

    Knoppi Active Member Licensed User

    I have to look at the regex next times. :)

    A very powerful function.
     
  4. Knoppi

    Knoppi Active Member Licensed User

    @Erel,
    your example fails
    Code:
    Dim s As String
    s =  
    "aäßüöáàâ"
    Log ( IsLetter (s)) 'return FALSE
    I think it must be
    Code:
    Sub IsLetter (c As StringAs Boolean
        
    Dim pattern As String = "\p{L}+"
        
    Return Regex.IsMatch( pattern, c)
    End Sub
    returns TRUE

    but i'm a newbie in RegEx
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    I've implemented IsLetter. Not IsLetters.

    If you want to implement IsLetters:
    Code:
    Sub IsLetters (text As StringAs Boolean
       
    Dim pattern As String = "\p{Alpha}+"
       
    If IsDevTool("B4J"Then pattern = "(?U)" & pattern
       
    Return Regex.IsMatch(pattern, text)
    End Sub
     
    Jmu5667, LipJ and jmon like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice