Board logo

标题: 求助:如何实现自动计算字符串的长度 [打印本页]

作者: longshou     时间: 2008-9-10 21:39    标题: 求助:如何实现自动计算字符串的长度

输入一串字符,就可自动计算字符串的长度,主要是计算字符串长度的那段命令想不出,哪位高人给详细分析一下,先谢谢各位了!
作者: pusofalse     时间: 2008-9-10 21:52
1.字符截取和移位
2.findstr /o
3.如果全是半角,可以写入文件,读取%%~za
作者: huahua0919     时间: 2008-9-10 21:53
这个方法很多,
@echo off
set/p l=please input a string:
:lp
set /a n+=1
set l=%l:~1%
if defined l goto :lp
echo string.len is %n%
pause

作者: HAT     时间: 2008-9-10 21:53

@echo off
set /p str=请输入:
set length=0
for /f "skip=1 delims=:" %%a in ('^(echo "%str%"^&echo.^)^|findstr /o ".*"') do set /a length=%%a-5
echo %length%

作者: HAT     时间: 2008-9-10 21:55

@echo off
set /p str=请输入:
set length=0
echo "%str%">"%temp%\strlen.txt"
for %%a in ("%temp%\strlen.txt") do set length=%%~za
set /a length=length-4
echo %length%

作者: s11ss     时间: 2008-9-10 22:11
vbs:
wsh.echo len(inputbox(""))

作者: longshou     时间: 2008-9-10 22:54
先谢谢楼上的各位
找到一个别人写的批处理
@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等号右边是什么意思?
作者: HAT     时间: 2008-9-10 22:59
for /?

  Quote:
   %~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


作者: longshou     时间: 2008-9-10 23:14
这是for命令里的用法吧,可那条命令是set theString=%~1,而且右边的变量是数字而不是字符,这样也可以吗?
作者: HAT     时间: 2008-9-11 10:08
call :stringlenth "%str%" num
调用stringlenth的时候,%1代表第一个参数,也就是"%str%"
作者: longshou     时间: 2008-9-11 20:56
明白了,谢谢HAT的指导,谢谢楼上各位的回复