我有一个变量,我试图只将大写字母变成不同的数字。
set var1=HeLlO
set var1=%var1:A=0%
set var1=%var1:B=1%
set var1=%var1:C=2%
set var1=%var1:D=3%
...
echo %var1%
但正在发生的事情是,当我希望它只转动大写字母L时,它将两个l变成一个数字。
set var1=%var1:A=1%
该代码将所有小写字母和全部大写字母As转换为1,但我希望它只将大写字母As转换为1s
答案 0 :(得分:1)
我从dbenham's ROT13获取了此代码。只需要评论一些事情。
@echo off
set var=AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz!
echo var is: %var%
call :rot13 var newvar
setlocal enabledelayedexpansion
echo var is: !newvar!
pause
goto :EOF
:rot13 StrVar [RtnVar] -- Applies the ROT13 cipher to a string
::
:: Applies the simple "rotate alphabet 13 places" cipher to the string
:: contained within variable StrVar.
::
:: Sets RtnVar=result
:: or displays result if RtnVar not specified
::
setlocal
set "NotDelayedFlag=!"
setlocal enableDelayedExpansion
REM set "upper=#AAN#BBO#CCP#DDQ#EER#FFS#GGT#HHU#IIV#JJW#KKX#LLY#MMZ#NNA#OOB#PPC#QQD#RRE#SSF#TTG#UUH#VVI#WWJ#XXK#YYL#ZZM#"
set "upper=#AA@#BB$#CC-#DD_#EE=#FF+#GG`#HH:#II;#JJ'#KK{#LL}#MM[#NN]#OO\#PP/#QQ,#RR.#SS~#TTÄ#UU?#VV<#WW>#XX|#YYÖ#ZZÜ#"
REM set "lower=#aan#bbo#ccp#ddq#eer#ffs#ggt#hhu#iiv#jjw#kkx#lly#mmz#nna#oob#ppc#qqd#rre#ssf#ttg#uuh#vvi#wwj#xxk#yyl#zzm#"
set "str=A!%~1!"
set "len=0"
for /L %%A in (12,-1,0) do (
set /a "len|=1<<%%A"
for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
)
set /a len-=1
set rtn=
for /l %%n in (0,1,!len!) do (
set "c=!%~1:~%%n,1!"
if "!c!" geq "a" if "!c!" leq "Z" (
for /f "delims=" %%c in ("!c!") do (
set "test=!upper:*#%%c=!"
if "!test:~0,1!"=="!c!" (
set c=!test:~1,1!
REM ) else (
REM set "test=!lower:*#%%c=!"
REM if "!test:~0,1!"=="!c!" set c=!test:~1,1!
)
)
)
set "rtn=!rtn!!c!"
)
if "%~2"=="" (
echo:!rtn!
exit /b
)
:: The remainder is "Jeb magic" used to transfer the rtn value across the function endlocal barrier
if defined rtn (
set "rtn=!rtn:%%=%%~A!"
set "rtn=!rtn:"=%%~B!"
if not defined NotDelayedFlag set "rtn=!rtn:^=^^^^!"
)
if defined rtn if not defined NotDelayedFlag set "rtn=%rtn:!=^^^!%" !
set "replace=%% """"
for /f "tokens=1,2" %%A in ("!replace!") do (
endlocal
endlocal
set "%~2=%rtn%" !
)
exit /b
这是输出。
var is: AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz!
var is: @a$b-c_d=e+f`g:h;i'j{k}l[m]n\o/p,q.r~s─t?u<v>w|x╓y▄z!
Press any key to continue . . .