无涯教程-批处理 - String length函数

136 阅读1分钟

在DOS脚本中,没有定义用于查找字符串长度的长度函数,有一些自定义函数可用于相同的功能,以下是一个自定义函数的示例,用于查看字符串的长度。

@echo off
set str = Hello World
call :strLen str strlen
echo String is %strlen% characters long
exit /b

:strLen setlocal enabledelayedexpansion

:strLen_Loop if not "!%1:~%len%!"=="" set /A len+=1 & goto :strLen_Loop (endlocal & set %2=%len%) goto :eof

关于上述程序,需要记住的一些关键事项是-

  • 在:strLen块中定义查找字符串长度的实际代码。

  • 字符串的长度保留在变量len中。

上面的命令产生以下输出。

11

参考链接

www.learnfk.com/batch-scrip…