|
看到鹤冲天兄提问set ii=%ii:~0,1% 的意思,意识到很多人对于变量的字符截取不了解,所以想起中国dos联盟里的相关帖子,特转帖过来,以飨同好。
单开一贴,一来是为挣点积分,二来那个帖子太长了,不便于大家阅读。
代码作者:lxmxn
- ::code by lxmxn @ cn-dos.net .
- ::目的:帮助新手们更好的理解变量字符截取功能.
- ::Date 2006-12-16 .
- ::说明:有的变量截取之后值为空,这是正常的.
- @echo off&setlocal EnableDelayedexpansion
- ::设置变量var的值:
- set var=0123456789ABCDEF
- call :begin
- for /l %%a in (1,3,15) do (
- for /l %%b in (1,4,15) do (
- call :printf %%a %%b
- )
- )
- call :begin
- for /l %%a in (1,4,15) do (
- for /l %%b in (-1,-3,-15) do (
- call :printf %%a %%b
- )
- )
- call :begin
- for /l %%a in (-2,-3,-15) do (
- for /l %%b in (1,4,15) do (
- call :printf %%a %%b
- )
- )
- call :begin
- for /l %%a in (-1,-4,-15) do (
- for /l %%b in (-2,-3,-15) do (
- call :printf %%a %%b
- )
- )
- pause
- endlocal&cls&echo.
- echo 演示结束了,任意键退出……
- pause>nul
- goto :eof
- ::输出结果:
- :printf
- echo %%var:~%1,%2%% 截取之后的字符为: !var:~%1,%2!
- goto :eof
- ::清理屏幕:
- :begin
- echo\&pause&cls&echo.
- echo 变量var的值为: %var%
- echo.
- goto :eof
ccwan发表于: 2007-02-04 08:47
为了格式的工整,特附上附件
[ 本帖最后由 ccwan 于 2007-2-4 08:53 AM 编辑 ] |
|