@echo off
setlocal ENABLEDELAYEDEXPANSION
set /p "str=请输入字符串:"
set /p "char=请输入要统计的字符(串):"
call:Length "%str%" "str_len"
call:Length "%char%" "char_len"
set /a "n=0"
set /a "count=0"
call:Find "%str%" "%char%" "%char_len%" "0" "pos"
if "%pos%"=="-1" (echo 没找到。&pause>nul&exit /b 1)
:loop
set /a "count+=1"
set /a "pos+=%char_len%"
call:Find "%str%" "%char%" "%char_len%" "%pos%" "pos"
if "%pos%"=="-1" goto:output
goto:loop
:output
echo 共有%count%个匹配项。
pause>nul
exit /b 0
:Length
:: Length [str] [var]
setlocal
set /a "_str_n=0"
set "__str=%~1"
:_len_func
if /i "!__str:~%_str_n%,1!"=="" goto:__end_func
set /a "_str_n+=1"
goto :_len_func
:__end_func
endlocal&set "%~2=%_str_n%"
goto:EOF
:Find
:: Find [strCheck] [strMatch] [len] [start] [var]
setlocal
set "_strCheck=%~1"&set /a "_match_len=%~3"&set "_strMatch=%~2"&set "_temp_str="
set /a "_pos=%~4"&set /a "_temp_pos_check=0"&set /a "_temp_pos_match=0"
:_find_loop
::ping /n 2 127.1>nul 2>nul
if /i "!_strCheck:~%_pos%,1!"=="" (set /a "_pos=-2"&goto:_end_find)
if /i "!_strCheck:~%_pos%,1!"=="!_strMatch:~0,1!" (
set /a "_pos-=1"
set "_temp_str=!_strCheck:~%_pos%,%_match_len%!"
if /i not "!_temp_str!"=="!_strMatch!" goto:_end_find else set /a "_pos+=1"
) else (set /a "_pos+=1"&goto:_find_loop)
:_end_find
endlocal&set /a "%~5=%_pos%+1"
goto:EOF
setlocal ENABLEDELAYEDEXPANSION
set /p "str=请输入字符串:"
set /p "char=请输入要统计的字符(串):"
call:Length "%str%" "str_len"
call:Length "%char%" "char_len"
set /a "n=0"
set /a "count=0"
call:Find "%str%" "%char%" "%char_len%" "0" "pos"
if "%pos%"=="-1" (echo 没找到。&pause>nul&exit /b 1)
:loop
set /a "count+=1"
set /a "pos+=%char_len%"
call:Find "%str%" "%char%" "%char_len%" "%pos%" "pos"
if "%pos%"=="-1" goto:output
goto:loop
:output
echo 共有%count%个匹配项。
pause>nul
exit /b 0
:Length
:: Length [str] [var]
setlocal
set /a "_str_n=0"
set "__str=%~1"
:_len_func
if /i "!__str:~%_str_n%,1!"=="" goto:__end_func
set /a "_str_n+=1"
goto :_len_func
:__end_func
endlocal&set "%~2=%_str_n%"
goto:EOF
:Find
:: Find [strCheck] [strMatch] [len] [start] [var]
setlocal
set "_strCheck=%~1"&set /a "_match_len=%~3"&set "_strMatch=%~2"&set "_temp_str="
set /a "_pos=%~4"&set /a "_temp_pos_check=0"&set /a "_temp_pos_match=0"
:_find_loop
::ping /n 2 127.1>nul 2>nul
if /i "!_strCheck:~%_pos%,1!"=="" (set /a "_pos=-2"&goto:_end_find)
if /i "!_strCheck:~%_pos%,1!"=="!_strMatch:~0,1!" (
set /a "_pos-=1"
set "_temp_str=!_strCheck:~%_pos%,%_match_len%!"
if /i not "!_temp_str!"=="!_strMatch!" goto:_end_find else set /a "_pos+=1"
) else (set /a "_pos+=1"&goto:_find_loop)
:_end_find
endlocal&set /a "%~5=%_pos%+1"
goto:EOF