I wrote the following procedure to determine whether a string contains only alphabetical characters as PB does not have a function for this purpose and I could not find a solution in the forum.
Procedure.l IsStrAlpha(*theString)
;=========================================================================================
; PURPOSE.... determines whether a string contains only alphabetical characters
; PARAMETER.. *theString - Address of the string to check
; RETURNS.... 1 if the string contains only alphabetical characters
;............ 0 if the string contains any non-alphabetical characters (including spaces)
;=========================================================================================
!mov eax, [p.p_theString] ; Get address of string in eax
!push esi ; preserve esi
!mov esi, eax ; copy address of string to esi
!cld ; makes esi count upwards when lodsw is used (hence fetches next character in string)
!sub eax, eax ; clear eax
!l_NextChar:
!lodsw ; get word pointed to by esi in ax - use word for Unicode strings [lodsb for ascii strings]
!test al,al ; check if low byte is zero ie is this end of string?
!jz l_IsAlpha ; if we have reached the end of the string then the string is alphanumeric
!cmp ax, 65 ; A=65 If ax is less than 65 then ax holds a non-alphabetical character
!jl l_NotAlpha
!cmp ax, 91 ; Z=90 If ax is less than 91 then ax holds an alphabetical character
!jl l_NextChar ; get next character to examine
!cmp ax, 97 ; a=97 If ax is less than 97 then ax holds a non-alphabetical character
!jl l_NotAlpha
!cmp eax, 123 ; 122 = z If ax is less than 123 then ax holds an alphabetical character
!jl l_NextChar ; get next character to examine
!l_NotAlpha: ; string contains a non-alphabetical character
!pop esi ; restore esi
!mov eax, 0
!jmp l_exit
!l_IsAlpha: ; string contains an alphabetical character
!pop esi ; restore esi
!mov eax, 1
!l_exit:
ProcedureReturn
EndProcedure
a$ = "12345"
Debug "String "+ a$ + " returns " + IsStrAlpha(@a$)
a$ = "thequickbrownfoxjumpsoverthelazydogs"
Debug "String "+ a$ + " returns " + IsStrAlpha(@a$)
a$="THEQUICKBROWNFOXJUMPSOVERTHELAZYDOG"
Debug "String "+ a$ + " returns " + IsStrAlpha(@a$)
a$ = "thequickbrownfoxjumpsoverthelazydog9"
Debug "String "+ a$ + " returns " + IsStrAlpha(@a$)
a$="THEQUICKBROWNFOXJUMPSOVERTHELAZYDOGS9"
Debug "String "+ a$ + " returns " + IsStrAlpha(@a$)
To test if a unicode string (ie. a string supporting international languages) comprises only alpha characters is, of course, a much bigger job than just for ASCII; and for that, I think it best to use API functions.
Here's a quick example for WINDOWS ONLY. Note that this code is not built for speed, just simplicity:
minimy wrote:but.. i try with 'especial' spanish character Ñ. and not work as espected
If you want to handle special characters as well, the fastest way is to use asm combined with a lookup table that indicates which characters are valid.
minimy wrote: but.. i try with 'especial' spanish character Ñ. and not work as espected.
...
X$ = "ABCabc Ñ" + Chr($00D6) + Chr($00DF) + Chr($0178) + Chr($019B) + Chr($1E40) + Chr($1FAF)
...
ABCabc ÑÖߟƛṀᾯ
0
It actually did work as expected - the space character is NOT an alpha character - you inserted a space character before the spanish character Ñ and that made the string not all alpha.
If you remove the space, it should work as expected: