1.

Solve : Check for case?

Answer»

Is there a simple way to check the VALUE of a variable, to determine if begins with an upper/lowercase letter? Or WOULD I have to do a case-sensitive check against a-z separately?

Thanks.Pipe the FIRST character of the variable string through findstr /R with the whole alphabet in upper case* as the regex. Then inspect the errorlevel.

*This avoids the findstr regex case bug (which is: in Windows after NT, ie 2k to 7 inclusive, findstr /r has a case sensitivity bug so that the regex for alphabetical ranges fails on all CHARS after the first so "[A-Z]" correctly rejects a but incorrectly passes b-z)

Code: [Select]echo off
:loop
set /p variable="Enter a string ? "
if "%variable%"=="ZZZ" goto end
echo %variable:~0,1% | findstr /r  "[ABCDEFGHIJKLMNOPQRSTUVWXYZ]">nul
if %errorlevel% equ 0 (
echo 1st char is upper case
) else (
echo 1st char is lower case
)
goto loop
:end


Code: [Select]S:\Test\Batch\>casetest.bat
Enter a string ? Hello
1st char is upper case
Enter a string ? hello
1st char is lower case
Enter a string ? cat
1st char is lower case
Enter a string ? Cat
1st char is upper case
Enter a string ? a
1st char is lower case
Enter a string ? A
1st char is upper case
Enter a string ? zzz

S:\Test\Batch\>

That worked. Thanks a lot.



Discussion

No Comment Found