Batch-script-string-length

提供:Dev Guides
移動先:案内検索

バッチスクリプト-文字列の長さ

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