如何遍历字符串中的令牌? [英] How to loop through tokens in a string?

查看:108
本文介绍了如何遍历字符串中的令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个字符串,比如foo:条:巴兹,是否可以遍历这个字符串?它看起来像你可以标记化文件的行,但下面将只能随声附和'富'一次。

FOR / F令牌= 1,2 * delims =%%倍In(%% J)做回声%% X


解决方案

 设置字符串= foo的:巴:巴兹
在(%字符串:: =%)做回声%%点¯x%%点¯x

有关值分隔符可以是空格,逗号,分号和等号。如果元素与任何字符分隔您可以直接处理的字符串。如果没有,只是改变的分隔符,这些字符中的一个(正如我上面一样)。

 组字符串= foo的酒吧,巴兹;一种=两个
在%% X(%字符串%)做回声%%点¯x

Say I have a string such as foo:bar:baz, is it possible to loop through this string? It looked like you could tokenize lines of a file but the following will only echo 'foo' once.

for /f "tokens=1,2* delims=:" %%x in ("%%j") do echo %%x

解决方案

set string=foo:bar:baz
for %%x in (%string::= %) do echo %%x

FOR value delimiters may be space, comma, semicolon and equal-sign. You may directly process a string if the elements are delimited with any of these characters. If not, just change the delimiter for one of these character (as I did above).

set string=foo bar,baz;one=two
for %%x in (%string%) do echo %%x

这篇关于如何遍历字符串中的令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆