如何计算与批次字符串中的字符? [英] How to count the characters in a string with Batch?

查看:156
本文介绍了如何计算与批次字符串中的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要计数批量的inputed字符串的字符。我不希望使用的临时文件。难道是没有他们做了什么?如果是的话,你的code的说明会大大AP preciated。太感谢!

I need to count the characters of an inputed string in Batch. I don't want to use temporary files. Could it be done without them? If yes, explanations of your code would be greatly appreciated. Thanks SO!

推荐答案

一个简单的方法是使用功能

A simple way is to use a function

@echo off
set "myVar=abcdefg"
call :Stringlength result myVar
echo %result%
exit /b

:Stringlength <resultVar> <stringVar>
(   
    setlocal EnableDelayedExpansion
    set "s=!%~2!#"
    set "len=0"
    for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
        if "!s:~%%P,1!" NEQ "" ( 
            set /a "len+=%%P"
            set "s=!s:~%%P!"
        )
    )
)
( 
    endlocal
    set "%~1=%len%"
    exit /b
)

这可以测量字符串为最多8192个字符,作为字符串的最大大小为8191字节,这应该是足够了。结果
第一个括号块仅适用于多一点的表现。结果
需要第二块返回%LEN%的ENDLOCAL屏障背后的价值。结果
其主要思想是一个二进制搜索,在第一循环中的临时副本中的字符串取值如果是大于4096字节或不再进行测试。结果
那么接下来的测试将与2048或6144(= 2048 + 4096),因此 LEN 变量将在每个循环多一点点精确。结果
经过13圈的LEN是准确的。

This can measure the string to a maximum of 8192 characters, as the maximum size of a string is 8191 bytes, this should be enough.
The first parenthesis blocks is only for a bit more performance.
The second block is needed to return the %len% value behind the endlocal barrier.
The main idea is a binary search, in the first loop the temporary copy in s of the string is tested if it is longer than 4096 bytes or not.
Then the next test will be with 2048 or 6144 (=2048+4096), so the len variable will be at each loop a little bit more exact.
After 13 loops the len is exact.

有关strlen的速度更快功能,你可以阅读 strlen的提振,它使用一些更多的花样。

For faster strlen functions you could read strlen boosted, which uses some more tricks.

还有一个批处理宏的解决方案,宏通常比使用批处理功能快得多。

There is also a solution with batch macros, macros are normally much faster than functions in batch.

@echo off
call :loadMacros
set "myVar=abcdefg"
%$strlen%  result myVar
echo %result%
exit /b


:loadMacros
set LF=^


::Above 2 blank lines are required - do not remove
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
:::: StrLen pResult pString
set $strLen=for /L %%n in (1 1 2) do if %%n==2 (%\n%
        for /F "tokens=1,2 delims=, " %%1 in ("!argv!") do (%\n%
            set "str=A!%%~2!"%\n%
              set "len=0"%\n%
              for /l %%A in (12,-1,0) do (%\n%
                set /a "len|=1<<%%A"%\n%
                for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"%\n%
              )%\n%
              for %%v in (!len!) do endlocal^&if "%%~b" neq "" (set "%%~1=%%v") else echo %%v%\n%
        ) %\n%
) ELSE setlocal enableDelayedExpansion ^& set argv=,

exit /b

目前dostips.com是关于宏观工艺结果的一些讨论
1 批宏带参数结果
2 宏与

At dostips.com are some discussion about the macro technic
1 Batch "macros" with arguments
2 macros with appended parameters

这篇关于如何计算与批次字符串中的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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