B4J Code Snippet isLetters - check if string are letters

Don't do something like this:
B4X:
'This code is WRONG!
Sub IsLetter(L As String) As Boolean
    return ( "ABCDEFGHIJKLMNOPQRSTUVWXYZ".Contains( L.ToUpperCase) )
End Sub
or this
B4X:
'Visual Basic Code
'This code is WRONG!
Function IsLetter(strValue As String) As Boolean
  Dim intPos As Integer  For intPos = 1 To Len(strValue)
    Select Case Asc(Mid(strValue, intPos, 1))
      Case 65 To 90, 97 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
https://docs.oracle.com/javase/tutorial/i18n/text/charintro.html

Library: JavaObject

B4X:
'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:
B4X:
Dim s As String = "aüßáàâăąæåã"
Log( isLetters( s))   'return true
 

Knoppi

Active Member
Licensed User
Longtime User
I have to look at the regex next times. :)

A very powerful function.
 

Knoppi

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

but i'm a newbie in RegEx
 
Top