Sub that returns 'a' or 'an'

U

unba1300

Guest
This works, but I know there must be a better way. Maybe using Type somehow?
B4X:
'-------------------------------------------------------------------------
'Returns 'a' or 'an' depending on if the passed string starts with a vowel.
'-------------------------------------------------------------------------
Sub DetermineAorAn (et As String) As String
  If et.CharAt(0) = "a" OR et.CharAt(0) = "e" OR et.CharAt(0) = "i" OR _
     et.CharAt(0) = "o" OR et.CharAt(0) = "u" OR et.CharAt(0) = "A" OR _
     et.CharAt(0) = "E" OR et.CharAt(0) = "I" OR et.CharAt(0) = "O" OR _ et.CharAt(0) = "U" Then
    Return "an "
  Else
    Return "a "
  End If
End Sub
 

Kiffi

Well-Known Member
Licensed User
Longtime User
a possible solution:
B4X:
Sub DetermineAorAn (et As String) As String

  If "AEIOU".Contains(et.ToUpperCase.CharAt(0)) Then
    Return "an "
  End If
   
  Return "a "
   
End Sub
Greetings ... Kiffi
 
Upvote 0
U

unba1300

Guest
Thanks kiffi; your solution works. Although I'll have to give this more thought, as thedesolatesoul points out. There are so many exceptions in English, I doubt it would be possible to correctly resolve 'a' or 'an' all the time without the use of a huge database or something. At least I'll work on trying to get it right most of the time.
 
Upvote 0
Top