0

在此代码中,我不能在变量“varname”中使用变量“%a”:

@echo of
set varname=stringhere
for /l %%a in (1,1,10) do (
    echo %varname:~1,%%a%
    ping -n 0.01 >nul
)

但它显示的是:

a%a%a%a%a%a%a%a%a%a%

我希望它显示为:

s st str stri strin string stringh stringhe stringher stringhere

因此,换句话说,我试图尽可能地优化我的代码,我想创建一个函数,一次打印出一个变量,但它不允许我以我想要的方式使用子字符串..

4

2 回答 2

3
@ECHO OFF
SETLOCAL
set varname=stringhere
for /l %%a in (1,1,10) do (
    CALL echo %%varname:~0,%%a%%
)
GOTO :EOF

请注意,字符串从 character 开始0

于 2013-11-05T07:41:55.893 回答
2

This should do what you want, including the leading space that it looks like you have in your desired output. Remove the REM below if you want to remove the leading space.

@echo off
setlocal enabledelayedexpansion
set varname=stringhere
set "OutString="
for /l %%a in (1,1,10) do set OutString=!OutString! !varname:~0,%%a!
REM set OutString=%OutString:~1%
echo.%OutString%
pause
于 2013-11-05T05:24:59.860 回答