批處理字串長度

在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

上一篇: 批處理字串 下一篇: 批處理數組