批处理文件计算字符串中字符的所有出现次数 [英] Batch File Count all occurrences of a character within a string

查看:104
本文介绍了批处理文件计算字符串中字符的所有出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个版本号 17.06.01.01,我想知道有多少条目被一个句点分割.

I have a version number 17.06.01.01 and I would like to know how many entries there are split by a period.

我的最后一段代码是;

setlocal ENABLEDELAYEDEXPANSION
for /F "tokens=1-10 delims=." %%a in ("17.09.01.04.03") do (
set /a "iCount+=1"

echo %%a, !iCount!
)
endlocal

我尝试了许多For"命令,但似乎每次都越来越远.

I've tried a host of 'For' commands but seem to be getting further away each time.

推荐答案

用空格(作为分隔符)替换每个点.然后计算令牌的数量:

replace every dot with a space (as a delimiter). Then count number of tokens:

@echo off
set "string=17.09.01.04.03"
set count=0
for %%a in (%string:.= %) do set /a count+=1
echo %count%

(如果字符串中有其他空格、逗号或制表符,可能会给出错误的结果,但应该适用于仅包含数字和点的版本字符串示例)

(May give false results, if there are other spaces, commas or tabs in the string, but should work nice for your example of version strings containing only numbers and dots)

@treintje:

echo off
set "string=1>7.0 9.0&1.04.0!3"
set count=0
:again
set "oldstring=%string%"
set "string=%string:*.=%"
set /a count+=1
if not "%string%" == "%oldstring%" goto :again
echo %count%

这篇关于批处理文件计算字符串中字符的所有出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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