返回列表 发帖

【练习-016】批处理判断字符串长度

呵呵大家也看到题目--基础练习。我不会出的太难的,这道题的思路应该会比较多的
不过我也还没开始做,希望大家积极参与哦。(最惨的就是只能加别人两分,郁闷)
习题1.1 字符串长度不超过80。不含特殊字符
             例 i like the bathome,because here is the batch of the world.
       1.2 字符串长度小于255,含有特殊字符,试求其长度。
            例 %%a%%!ver!&^^^ ^*~                     are you o                 k ?" : \  / ` `  verfdxcweippo opj x ds gw !%%
一般的,先求1.1的题目。看谁的代码灵活、简洁、思路清晰。然后再做1.2


(汗。中文不用考虑……)
其实我也是想征集一个比较好的办法……估计如果有中文的话。或许可以重定向到文本然后再判断字节吧。我猜可以的。

最后:一题多解,可别只用 重定向文本 那个方法哈。

[ 本帖最后由 batman 于 2008-8-25 14:19 编辑 ]
[url=][/url]

感觉这题不是基础练习。
心绪平和,眼藏静谧。

TOP

额……感觉也是哦……不过重定向文本的方法很简便。(我觉得这个方法可能可以解决那个--三列对齐的问题)
别的方法就麻烦喽。
给个生成文本的。
@echo off
echo,i like the bathome,because here is the batch of the world.>x.x
for /f "tokens=*" %%a in ('dir x.x /-c ^|find " 字节" ^|find /v ":"') do (
for /f "tokens=3" %%b in ('echo,%%a') do (
set /a n=%%b-2
call echo %%n%%
del x.x
)
)
pause>nulCOPY

[ 本帖最后由 523066680 于 2008-8-14 09:06 编辑 ]
1

评分人数

[url=][/url]

TOP

*********************
屏蔽,看错题意。

[ 本帖最后由 pusofalse 于 2008-8-14 09:11 编辑 ]
心绪平和,眼藏静谧。

TOP

- -!

到底是判断i like the bathome,because here is the batch of the world.这段话里面除了空格之外还有多少个字符,还是判断这个保存为文本后有多大?

TOP

先1.1题,1.2题有点难度呵:
@echo off
set str=i like the bathome,because here is the batch of the world.
:count
if "%str%"=="" (echo %n%) else (set /a n+=1&set str=%str:~1%&goto count)
pauseCOPY

[ 本帖最后由 shqf 于 2008-8-15 07:19 编辑 ]
2

评分人数

TOP

1.2题果真有点难度。
从文本中取值还好,但直接给定一字符串,大概只能就题解题了。
心绪平和,眼藏静谧。

TOP

1.1
@echo off
set str=i like the bathome,because here is the batch of the world.
for /f "skip=1 delims=:" %%a in ('^(echo "%str%"^&echo.^)^|findstr /o ".*"') do set /a length=%%a-5
echo %length%COPY
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

1.1
@echo off
echo "i like the bathome,because here is the batch of the world.">"%temp%\_strlen.txt"
for %%a in ("%temp%\_strlen.txt") do set length=%%~za
set /a length=length-4
echo %length%COPY
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

哈 又学到了东西 感谢前辈们!

[ 本帖最后由 523066680 于 2008-8-15 09:26 编辑 ]
[url=][/url]

TOP

1.1:
@echo off
set "str=i like the bathome,because here is the batch of the world."
set "num=80"
:lp
set "str=%str%0"&set /a num-=1
if "%str:~79,1%" neq "0" goto lp
echo 字符个数为%num%个&pause>nulCOPY
@echo off
echo i like the bathome,because here is the batch of the world.>1.txt&echo.>>1.txt
for /f "delims=:" %%i in ('findstr /o .* 1.txt') do set "num=%%i"
set /a num-=2&del /q 1.txt
echo 字符个数为%num%个&pause>nulCOPY

[ 本帖最后由 batman 于 2008-8-15 10:00 编辑 ]
***共同提高***

TOP

1.2:
@echo off
set "str=%%a%%!ver!&^^^ ^*~                     are you o                 k ?" : \  / ` `  verfdxcweippo opj x ds gw !%%"
setlocal enabledelayedexpansion
set "num=255"
:lp
set "str=!str! "&set /a num-=1
if "!str:~254,1!" neq " " goto lp
echo 字符个数为%num%个&pause>nul
pause>nulCOPY

[ 本帖最后由 batman 于 2008-8-15 10:09 编辑 ]
1

评分人数

***共同提高***

TOP

::%%a%%!ver!&^^^ ^*~                     are you o                 k ?" : \  / ` `  verfdxcweippo opj x ds gw !%%
@echo off&setlocal enabledelayedexpansion
set /p str=<%0
for /l %%a in (1,1,255) do (set str=!str:~1!&if "!str!"=="" (set /a totle=%%a-2&echo !totle!&goto end))
:end
pauseCOPY
应该是111个字符吧,上楼的结果怎么是108呀?

[ 本帖最后由 shqf 于 2008-8-15 15:28 编辑 ]
2

评分人数

TOP

回复 13楼 的帖子

12楼在第一次set的时候,脱掉了三个百分号。
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

13楼的方法不错,我也这样写过,但缺点在于很难作为一个函数或者模块放到一个大的批处理里面,缺乏实用性,不知道 shqf 兄有没有好的方法?
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

返回列表