[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[其他] 批处理怎样高效率统计出变量值中字母和数字的个数?

本帖最后由 pcl_test 于 2016-9-13 19:29 编辑

例:
set /p AA=变量:
检查变量AA中包含有多少个英文和数字字符?我自己做了一个效率太低.

@echo off&&setlocal ENABLEDELAYEDEXPANSION
set BLV=0&set BLU=0
set /P BLE=变量输入:
:BQM
if not "!BLE:~%BLV%,1!"=="" echo 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ|find "!BLE:~%BLV%,1!">nul&&set /A BLU=%BLU%+1
if not "!BLE:~%BLV%,1!"=="" set /A BLV=%BLV%+1&&goto BQM
echo %BLU%
pause
这个是我写的代码,一个一个字母的判定,所以效率低。

TOP

  1. @echo off
  2. set "str=abcdefghijklmnopqrstuvwxyz0123456789%~1"
  3. call :calc tmp
  4. goto :eof
  5. :calc
  6. setlocal enabledelayedexpansion
  7. >tmp echo\!str!
  8. set s1=%~z1
  9. for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do set "str=!str:%%a=!"
  10. >tmp echo\!str!
  11. set s2=%~z1
  12. for /l %%a in (0,1,9) do set "str=!str:%%a=!"
  13. >tmp echo\!str!
  14. set s3=%~z1
  15. set /a E=s1-s2-26,N=s2-s3-10
  16. echo E:%E%,N:%N%
  17. del /f /q tmp
复制代码
真寂寞结果2L的是最快的。。
第三方命令行工具编程
Http://Hi.Baidu.Com/Console_App

TOP

本帖最后由 defanive 于 2011-11-15 06:42 编辑
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do set v%%a=10000
  4. for /l %%a in (0,1,9) do set v%%a=1
  5. set /p str=
  6. echo !str!>tmp
  7. for %%a in (tmp) do set /a size=%%~za-3
  8. del /f /q tmp
  9. for /l %%a in (0,1,%size%) do set "exec=!exec!v!str:~%%a,1!+"
  10. 2>nul set /a t+=%exec%0
  11. set /a E=t/10000,N=t%%10000
  12. echo E:%E%,N:%N%
  13. pause
复制代码
我也来献丑一段
相对比较快,但是有字数限制,貌似2700左右
第三方命令行工具编程
Http://Hi.Baidu.Com/Console_App

TOP

本帖最后由 raymai97 于 2011-11-15 02:29 编辑

献丑了,special就是代表那些空格、逗号、句号
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set /p a=Enter:
  4. set skip=0
  5. set /a text=0,num=0,special=0
  6. if "!a!"=="" goto result
  7. :loop
  8. if "!a:~%skip%,1!" lss "a" (
  9. if "!a:~%skip%,1!" lss "0" (
  10. set /a special+=1) else (set /a num+=1)
  11. ) else (set /a text+=1)
  12. set /a skip+=1
  13. if not "!a:~%skip%,1!"=="" goto loop
  14. :result
  15. echo text: %text%
  16. echo num: %num%
  17. echo special: %special%
  18. pause>nul
复制代码
为何批处理不适合做界面
为何随风讨厌call命令
http://bbs.bathome.net/thread-4482-1-10.html

TOP

把你写的贴出来看看先
别人也好帮你优化

TOP

本帖最后由 weichenxiehou 于 2011-11-14 20:37 编辑

回复 1# heyoug
效率也不高,呵呵,仅供娱乐。
  1. @echo off&setlocal enabledelayedexpansion
  2. set/p str=input a string:
  3. :again
  4. if not "!str!"=="" echo,!str:~-1!>>temp.tmp&set "str=!str:~,-1!"&goto :again
  5. set/p=letters:<nul&findstr "[a-zA-Z] " temp.tmp|find /v /c ""
  6. set/p=numbers:<nul&findstr "[0-9] " temp.tmp|find /v /c ""
  7. del temp.tmp&pause>nul
复制代码
看得多说得多,远比不上写得多。

TOP

计算原字符串长度,然后替换掉所有英文和数字,再计算长度...

TOP

返回列表