[英]Can option of delims in For / f be a string?
My code is as follows: 我的代码如下:
for /F "tokens=1,2 delims=%0.3%" %%i in ("double a = 0.3;") do ( 对于/ F“ tokens = 1,2 delims =%0.3%” %% i in(“ double a = 0.3;”)执行(
set a=%%i 设置a = %% i
set b=%%j 设置b = %% j
) )
I want a = "double a = " and b =";" 我想要一个=“ double a =”和b =“;” but this code can not help. 但是这段代码无济于事。
Does anyone know how to solve this problem? 有谁知道如何解决这个问题?
Your code as posted simply will not work at all. 您发布的代码根本无法使用。
for /F "tokens=1,2 delims=%0.3%" %%i in ("double a = 0.3;") do (
The %0
will be replaced by yourbatchfilename
and since %" %
has no value set in the environment, it will be replaced by [nothing] yielding %0
将由yourbatchfilename
替换,并且由于%" %
在环境中未设置值,它将由[nothing]替换
for /F "tokens=1,2 delims=yourbatchfilename.3i in ("double a = 0.3;") do (
which, unsurprisingly, is a syntax error. 毫不奇怪,这是语法错误。
@ECHO OFF
SETLOCAL
for /F "tokens=1,2 delims=3.0" %%i in ("double a = 0.3;") do (
set a=%%i
set b=%%j
)
ECHO first way :a=%a%+ b=%b%+
ENDLOCAL
SETLOCAL
SET "astring=double a = 0.3;"
SET "aseparator=0.3"
SET "arotorapes=3.0"
for /F "tokens=1,2 delims=%arotorapes%" %%i in ("double a = 0.3;") do (
SET a=%%i
)
CALL SET b=%%astring:*%aseparator%=%%
ECHO second way :a=%a%+ b=%b%+
ENDLOCAL
SETLOCAL
SET "astring=double a = 0.3;"
SET "aseparator=0.3"
CALL SET b=%%astring:*%aseparator%=%%
SET "c=%b%%aseparator%"
SET "a=%astring%"
:loop
IF DEFINED c SET c=%c:~0,-1%&SET a=%a:~0,-1%&GOTO loop
ECHO third way :a=%a%+ b=%b%+
In the first method, I've changed the sequence of the characters to deomstrate that any sequence of any of the characters between the =
and "
of a delims
clause acts as a SINGLE delimiter. The code "works" by failing to fail. 在第一种方法中,我更改了字符的顺序以消除delims
子句的=
和"
之间的任何字符的任何序列都充当单个定界符。代码“工作”失败了。
In the second method, the setting of a
works in the same way. 在第二种方法中,设置a
以相同的方式工作。 Without further information, it's really not possible to tell whether this is adequate. 没有更多的信息,实际上就无法确定这是否足够。 b
is set by specifically replacing all characters in the source string up to and including the separator with [nothing] 通过专门用[nothing]替换源字符串中的所有字符,直到并包括分隔符,来设置b
In the third method, the same operation establishes b
, and then b
and the separator are concatenated. 在第三种方法中,相同的操作建立b
,然后将b
和分隔符连接起来。 Systematically lop off the characters at the end of a copy of the original original string and the concatenated string. 系统地删除原始原始字符串和串联字符串副本末尾的字符。 When the concatenated version becomes empty, you've deleted the separator and the appendix from the original, leaving the portion of the original string up to the separator. 当串联的版本变为空时,您已从原始副本中删除了分隔符和附录,而将原始字符串的一部分留给了分隔符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.