计算CMD和批处理文件中的字母数

时间:2015-03-13 02:46:50

标签: windows batch-file cmd

我有这个项目使用批处理文件。我的代码可以在用户输入数字(数字,字母,符号)时计算字符,但我希望程序只计数字母。你能解决这个问题吗?我的代码如下所示。

@ECHO OFF
echo.

:a
REM Set "string" variable
SET /p string="Type characters to Count:"
REM Set the value of temporary variable to the value of "string" variable
SET temp_str=%string%
REM Initialize counter
SET str_len=1

:loop
if defined temp_str (
REM Remove the first character from the temporary string variable and increment 
REM counter by 1. Countinue to loop until the value of temp_str is empty string.
SET temp_str=%temp_str:~1%
SET /A str_len += 1
GOTO loop
)

REM Echo the actual string value and its length.
ECHO %string% is %str_len% characters long!
set /p prompt="Do you want to continue?Y/N:"
if %prompt%==Y goto a
goto b

:b
pause
exit

3 个答案:

答案 0 :(得分:2)

@ECHO OFF
    setlocal enableextensions disabledelayedexpansion

:getString
    echo(
    REM Set "string" variable
    set "string="
    SET /p "string=Type characters to Count:"

:calculateLength
    if not defined string ( set "str_len=0" ) else (
        for /f %%a in ('
            cmd /u /v /q /c"(echo(!string!)" %= output unicode string =%
            ^| find /v ""                    %= separate lines =%
            ^| findstr /r /c:"[a-zA-Z]"      %= filter characters =%
            ^| find /c /v ""                 %= count remaining lines =%
        ') do set "str_len=%%a"
    )

:show    
    REM Echo the actual string value and its length.
    ECHO [%string%] is %str_len% letters long!
    echo(
    set /p "prompt=Do you want to continue? Y/n: " || set "prompt=Y"
    if /i "%prompt%"=="Y" goto :getString

    pause
    exit /b

这使用了一个简单的技巧。如果我们启动一个unicode cmd实例,它的输出(在这种情况下来自echo的结果)是unicode,即每个字符两个字节,通常是null和真实字符。

此输出由过滤器morefind处理,它将空值作为行终止符处理,将输出字符串拆分为行,每个输入字符划分一行。

这组行使用findstr进行过滤,仅允许使用字母。其余行使用find /c /v ""命令计数,该命令将计算非空行。

已修改以适应评论。

输入一行并处理它们

@ECHO OFF
    setlocal enableextensions disabledelayedexpansion
    set "tempFile=%temp%\%random%%random%%random%.tmp"

:getString
    echo(
    echo(Please, type your text and end input pressing F6 and Enter
    copy con "%tempfile%" >nul 2>nul

:calculateLength
    for /f %%a in ('
        cmd /u /q /c"(type "%tempFile%")" %= output unicode string =%
        ^| find /v ""                     %= separate lines =%
        ^| findstr /r /c:"[a-zA-Z]"       %= filter characters =%
        ^| find /c /v ""                  %= count remaining lines =%
    ') do set "str_len=%%a"

:show    
    ECHO input data is %str_len% letters long!
    echo(
    del /q "%tempFile%" 2>nul 
    set /p "prompt=Do you want to continue? Y/n: " || set "prompt=Y"
    if /i "%prompt%"=="Y" goto :getString

    pause
    exit /b

答案 1 :(得分:0)

您正在将行初始字符串长度设置为1

SET str_len=1

将其更改为0,如下所示:

SET str_len=0

答案 2 :(得分:0)

@ECHO OFF
setlocal EnableDelayedExpansion

REM Define a string with all letters:
SET letters=ABCDEFGHIJKLMNOPQRSTUVWXYZ

echo.

:a
REM Set "string" variable
SET /p string="Type characters to Count:"
REM Set the value of temporary variable to the value of "string" variable
SET temp_str=%string%
REM Initialize counter
SET str_letters=0

:loop
if defined temp_str (
REM Get the first character from temporary string variable
SET char=%temp_str:~0,1%
REM If the character is letter, count it
if "!letters:%char%=!" neq "%letters%" SET /A str_letters += 1
REM Remove the first character from the temporary string variable.
REM Countinue to loop until the value of temp_str is empty string.
SET temp_str=%temp_str:~1%
GOTO loop
)

REM Echo the actual string value and its number of letters.
ECHO %string% have %str_letters% letters
set /p prompt="Do you want to continue?Y/N:"
if %prompt%==Y goto a
goto b

:b
pause
exit