中国DOS联盟论坛

中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --
联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum
游客 | 登录 | 注册 | 会员 | 搜索 | 中国DOS联盟
中国DOS联盟论坛
现在时间是 2026-08-09 17:41
47,811 主题排行 / 349,896 发帖 / 今日 1 篇 / 48,254 会员排行
DOS批处理 & 脚本技术(批处理室) » 求助:如何实现自动计算字符串的长度
可打印版本  1,133 / 10
第1楼 longshou 发表于 2008-09-10 21:39
初级用户 发帖 16 积分 30
求助:如何实现自动计算字符串的长度
输入一串字符,就可自动计算字符串的长度,主要是计算字符串长度的那段命令想不出,哪位高人给详细分析一下,先谢谢各位了!
第2楼 pusofalse 发表于 2008-09-10 21:52
银牌会员 发帖 646 积分 1,604
1.字符截取和移位
2.findstr /o
3.如果全是半角,可以写入文件,读取%%~za
第3楼 huahua0919 发表于 2008-09-10 21:53
银牌会员 发帖 780 积分 1,608
这个方法很多,
第4楼 HAT 发表于 2008-09-10 21:53
版主 发帖 5,017 积分 9,023
第5楼 HAT 发表于 2008-09-10 21:55
版主 发帖 5,017 积分 9,023
第6楼 s11ss 发表于 2008-09-10 22:11
银牌会员 发帖 566 积分 2,098
vbs:
第7楼 longshou 发表于 2008-09-10 22:54
初级用户 发帖 16 积分 30
先谢谢楼上的各位
找到一个别人写的批处理
@echo off
set /p str=请输入任意长度的字符串:
echo 你输入了字符串:"%str%"
call :stringlenth "%str%" num
echo 字符串长度为:%num%
pause
exit

:StringLenth
::---------字符串长度计算子程序
::---------参数%1为字符串(如有空格,请用引号括起来)
::---------参数%2为返回变量名称,不能含空格或特殊字符
::@echo off
set theString=%~1
if not defined theString goto :eof
set Return=0

:StringLenth_continue
set /a Return+=1
set thestring=%thestring:~0,-1%
if defined thestring goto StringLenth_continue
if not "%2"=="" set %2=%Return%
goto :eof
关于计算字符长度的那段有几个地方不懂,就是set theString=%~1
if not defined theString goto :eof
set Return=0
这一部分有点不太懂set theString=%~1等号右边是什么意思?
第8楼 HAT 发表于 2008-09-10 22:59
版主 发帖 5,017 积分 9,023
for /?

%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
If the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string
第9楼 longshou 发表于 2008-09-10 23:14
初级用户 发帖 16 积分 30
这是for命令里的用法吧,可那条命令是set theString=%~1,而且右边的变量是数字而不是字符,这样也可以吗?
第10楼 HAT 发表于 2008-09-11 10:08
版主 发帖 5,017 积分 9,023
call :stringlenth "%str%" num
调用stringlenth的时候,%1代表第一个参数,也就是"%str%"
第11楼 longshou 发表于 2008-09-11 20:56
初级用户 发帖 16 积分 30
明白了,谢谢HAT的指导,谢谢楼上各位的回复
[ 联系联盟系统管理团队 - 中国DOS联盟 - 标准版 ]
Sponsored by ifanr Inc | © 2001–2023