在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