Following on from my previous thread containing functions enabling you to
format strings in according to various needs (such as converting a keycode into the name of the key, or a string representation of a boolean value).
I am posting some more string functions on this page.
IsNumeric( text$ )
Returns true if the text is a number
IsAlphabetic( text$ )
Returns true if the text contains characters from A to Z
IsAlphaNumeric( text$ )
Returns true if the text contains characters from A to Z or numbers
Example:
Print IsAlphabetic( "ABC" ) ` Is True
Print IsAlphabetic( "xyz" ) ` Is True
Print IsAlphabetic( "xyz#" ) ` Is False
Print "====================="
Print IsNumeric( "123" ) ` Is True
Print IsNumeric( "-5.6" ) ` Is True
Print IsNumeric( "room100" ) ` Is False
Print "====================="
Print IsAlphaNumeric( "abc123" ) ` Is True
Print IsAlphaNumeric( "123abc" ) ` Is True
Print IsAlphaNumeric( "123a..&^" ) ` Is False
Replace [Fast Len] with [Len] if you do not have Matrix1 utilities installed. This principle applies to similar alternative functions such as [Fast Lower$] and [Lower$].
//=======================================================
Function IsAlphaNumeric( sText$ )
Local bAlphabetic as Boolean :
Local iLen : iLen = Fast Len(sText$)
Local c$ : Local iChar
For i = 1 to iLen
bAlphabetic = 1
c$ = Mid$( sText$, i )
iChar = Asc( Fast Lower$( c$ ) )
If iChar < 97
bAlphabetic = 0
Else
If iChar > 122 Then bAlphabetic = 0
EndIf
If bAlphabetic = 0
If IsNumeric( c$ ) = 0
ExitFunction 0
Endif
If c$ = "." or c$ = "-" then ExitFunction 0
EndIf
Next i
EndFunction 1
//=======================================================
Function IsAlphabetic( sText$ )
Local bAlphabetic as Boolean
Local iLen : iLen = Fast Len(sText$)
Local c$ : Local iChar
For i = 1 to iLen
bAlphabetic = 1
c$ = Mid$( sText$, i )
iChar = Asc( Fast Lower$( c$ ) )
If iChar < 97
bAlphabetic = 0
Else
If iChar > 122 Then bAlphabetic = 0
EndIf
If bAlphabetic = 0
ExitFunction 0
EndIf
Next i
Endfunction 1
//=======================================================
Function IsNumeric( sText$ )
Local bNumeric as Boolean
Local iLen : iLen = Fast Len(sText$)
Local c$ : Local iChar
Local iDots
Local iMinusSigns
sText$ = Fast Lower$( sText$ )
For i = 1 to iLen
bNumeric = 1
iChar = Asc( Mid$( sText$, i ) )
If iChar = 46
Inc iDots
If iDots > 1 Then ExitFunction 0
Else
If iChar = 45
Inc iMinusSigns
If iMinusSigns > 1 Then ExitFunction 0
Else
If iChar < 48
bNumeric = 0
Else
If iChar > 57 Then bNumeric = 0
EndIf
If bNumeric = 0 Then ExitFunction 0
Endif
Endif
Next i
Endfunction 1
