[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
看这个十年前的老帖 http://www.bathome.net/thread-11799-1-1.html 可见当年批处理计算字符串长度的"技法峰值"
在此分享源网站 https://www.dostips.com 的这段经典代码(见以下代码段),其内还有两枚技术硬核》
一。句式 set "str=a!%~1!" 提高形参 %1 的容错率,一网打尽键盘所有可见字符
二。句式 (endlocal ... set /a %~2=%len%) 令局部变量亡前可续命给全局变量

附加几行代码》针对经典代码的测试/用法
  1. @echo off
  2. :[Loop] //测试代码 备注》调用子过程的形参须是变量名
  3. set "str=" &set/p str="输入一个字符串获取其长度:"
  4. if not defined str exit/b
  5. (call :strLen str sL)
  6. echo,长度=%sL%
  7. goto[Loop]
  8. :: 分享计算字符串长度的经典代码如下》
  9. ::      string [in]  - variable name containing the string being measured for length
  10. ::      len [out] - variable to be used to return the string length
  11. :: Many thanks to 'sowgtsoi', but also 'jeb' and 'amel27' dostips forum users helped making this short and efficient
  12. :: Created 20081122,changed 20101116,source https://www.dostips.com
  13. :strLen string len -- returns the length of a string
  14. (   setlocal enabledelayedexpansion
  15.     set "str=a!%~1!" &rem keep the a up front to ensure we get the length and not the upper bound,it also avoids trouble in case of empty string
  16.     set "len=0"
  17.     for /l %%a in (12,-1,0) do (
  18.         set /a "len|=1<<%%a"
  19.         for %%b in (!len!) do if "!str:~%%b,1!"=="" set /a "len&=~1<<%%a"
  20.     )
  21. )
  22. ( endlocal & rem return values
  23.     if "%~2" neq "" set /a %~2=%len%
  24. )
  25. exit /b
复制代码

TOP

回复 31# aloha20200628
谢谢提供链接,内容确实精彩!!!

TOP

回复 31# aloha20200628
谢谢,正想找这种代码呢!
本人已死,不用联系,要联系下来联系~

TOP

回复 31# aloha20200628
大佬,最近我又看到了Batcher大佬的多行回退,但是并未在win10中实现,不知为什么,

TOP

返回列表