

InterviewSolution
Saved Bookmarks
1. |
Solve : Check for case? |
Answer» <html><body><p>Is there a simple way to check the <a href="https://interviewquestions.tuteehub.com/tag/value-238057" style="font-weight:bold;" target="_blank" title="Click to know more about VALUE">VALUE</a> of a variable, to determine if begins with an upper/lowercase letter? Or <a href="https://interviewquestions.tuteehub.com/tag/would-3285927" style="font-weight:bold;" target="_blank" title="Click to know more about WOULD">WOULD</a> I have to do a case-sensitive check against a-z separately?<br/><br/>Thanks.Pipe the <a href="https://interviewquestions.tuteehub.com/tag/first-461760" style="font-weight:bold;" target="_blank" title="Click to know more about FIRST">FIRST</a> character of the variable string through findstr /R with the whole alphabet in upper case* as the regex. Then inspect the errorlevel. <br/><br/>*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 <a href="https://interviewquestions.tuteehub.com/tag/chars-914558" style="font-weight:bold;" target="_blank" title="Click to know more about CHARS">CHARS</a> after the first so "[A-Z]" correctly rejects a but incorrectly passes b-z)<br/><br/> Code: <a>[Select]</a>echo off<br/>:loop<br/>set /p variable="Enter a string ? "<br/>if "%variable%"=="<a href="https://interviewquestions.tuteehub.com/tag/zzz-750802" style="font-weight:bold;" target="_blank" title="Click to know more about ZZZ">ZZZ</a>" goto end<br/>echo %variable:~0,1% | findstr /r "[ABCDEFGHIJKLMNOPQRSTUVWXYZ]">nul<br/>if %errorlevel% equ 0 (<br/> echo 1st char is upper case<br/>) else (<br/> echo 1st char is lower case<br/>)<br/>goto loop<br/>:end<br/><br/><br/> Code: <a>[Select]</a>S:\Test\Batch\>casetest.bat<br/>Enter a string ? Hello<br/>1st char is upper case<br/>Enter a string ? hello<br/>1st char is lower case<br/>Enter a string ? cat<br/>1st char is lower case<br/>Enter a string ? Cat<br/>1st char is upper case<br/>Enter a string ? a<br/>1st char is lower case<br/>Enter a string ? A<br/>1st char is upper case<br/>Enter a string ? zzz<br/><br/>S:\Test\Batch\><br/><br/>That worked. Thanks a lot.</p></body></html> | |