计算CMD和批处理文件中的字母数 [英] Count number of letters in CMD and batchfile

查看:81
本文介绍了计算CMD和批处理文件中的字母数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用批处理文件的项目.下面的代码可以在用户输入内容(数字,字母,符号)时对字符进行计数,但是我希望程序仅对字母进行计数.您能帮我解决这个问题吗?我的代码如下所示.

I have this project using batch file.My code below can count characters when user type inputs (numbers, letters, symbols),but i want the program count only letters. Can you help me out with this problem. My code is shown below.

@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

推荐答案

@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和实字符.

This uses a simple trick. If we start a unicode cmd instance, its output (the result from echo in this case) is unicode, that is, two bytes per character, usually a null and the real character.

此输出由 more find 过滤器处理,该过滤器会将null用作行终止符,将输出字符串拆分为行,每个输入一行字符.

This output is processed by a filter, more or find, that will process the nulls as line terminators, splitting the output string into lines, one line for each input character.

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

This set of lines are filtered with findstr to only allow letters. The remaining lines are counted with a find /c /v "" command that will count non empty lines.

已编辑以适应评论.

输入一行并进行处理

@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

这篇关于计算CMD和批处理文件中的字母数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆