找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 165118|回复: 74

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

[复制链接]
发表于 2008-8-14 07:49:47 | 显示全部楼层 |阅读模式
呵呵大家也看到题目--基础练习。我不会出的太难的,这道题的思路应该会比较多的
不过我也还没开始做,希望大家积极参与哦。(最惨的就是只能加别人两分,郁闷)
习题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 编辑 ]
发表于 2008-8-14 08:10:47 | 显示全部楼层
感觉这题不是基础练习。
 楼主| 发表于 2008-8-14 08:20:02 | 显示全部楼层
额……感觉也是哦……不过重定向文本的方法很简便。(我觉得这个方法可能可以解决那个--三列对齐的问题)
别的方法就麻烦喽。
给个生成文本的。
  1. @echo off
  2. echo,i like the bathome,because here is the batch of the world.>x.x
  3. for /f "tokens=*" %%a in ('dir x.x /-c ^|find " 字节" ^|find /v ":"') do (
  4. for /f "tokens=3" %%b in ('echo,%%a') do (
  5. set /a n=%%b-2
  6. call echo %%n%%
  7. del x.x
  8. )
  9. )
  10. pause>nul
复制代码

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

评分

参与人数 1技术 +1 收起 理由
Hello123World + 1 乐于助人

查看全部评分

发表于 2008-8-14 08:31:38 | 显示全部楼层
*********************
屏蔽,看错题意。

[ 本帖最后由 pusofalse 于 2008-8-14 09:11 编辑 ]
发表于 2008-8-14 20:18:57 | 显示全部楼层
- -!

到底是判断i like the bathome,because here is the batch of the world.这段话里面除了空格之外还有多少个字符,还是判断这个保存为文本后有多大?
发表于 2008-8-15 07:16:02 | 显示全部楼层
先1.1题,1.2题有点难度呵:
  1. @echo off
  2. set str=i like the bathome,because here is the batch of the world.
  3. :count
  4. if "%str%"=="" (echo %n%) else (set /a n+=1&set str=%str:~1%&goto count)
  5. pause
复制代码

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

评分

参与人数 2PB +2 技术 +1 收起 理由
Hello123World + 1 算法巧妙!
523066680 + 2

查看全部评分

发表于 2008-8-15 08:00:08 | 显示全部楼层
1.2题果真有点难度。
从文本中取值还好,但直接给定一字符串,大概只能就题解题了。
发表于 2008-8-15 08:39:15 | 显示全部楼层
1.1
  1. @echo off
  2. set str=i like the bathome,because here is the batch of the world.
  3. for /f "skip=1 delims=:" %%a in ('^(echo "%str%"^&echo.^)^|findstr /o ".*"') do set /a length=%%a-5
  4. echo %length%
复制代码
发表于 2008-8-15 08:42:10 | 显示全部楼层
1.1
  1. @echo off
  2. echo "i like the bathome,because here is the batch of the world.">"%temp%\_strlen.txt"
  3. for %%a in ("%temp%\_strlen.txt") do set length=%%~za
  4. set /a length=length-4
  5. echo %length%
复制代码
 楼主| 发表于 2008-8-15 09:23:06 | 显示全部楼层
哈 又学到了东西 感谢前辈们!

[ 本帖最后由 523066680 于 2008-8-15 09:26 编辑 ]
发表于 2008-8-15 09:54:59 | 显示全部楼层
1.1:
  1. @echo off
  2. set "str=i like the bathome,because here is the batch of the world."
  3. set "num=80"
  4. :lp
  5. set "str=%str%0"&set /a num-=1
  6. if "%str:~79,1%" neq "0" goto lp
  7. echo 字符个数为%num%个&pause>nul
复制代码

  1. @echo off
  2. echo i like the bathome,because here is the batch of the world.>1.txt&echo.>>1.txt
  3. for /f "delims=:" %%i in ('findstr /o .* 1.txt') do set "num=%%i"
  4. set /a num-=2&del /q 1.txt
  5. echo 字符个数为%num%个&pause>nul

复制代码

[ 本帖最后由 batman 于 2008-8-15 10:00 编辑 ]
发表于 2008-8-15 10:08:31 | 显示全部楼层
1.2:
  1. @echo off
  2. set "str=%%a%%!ver!&^^^ ^*~                     are you o                 k ?" : \  / ` `  verfdxcweippo opj x ds gw !%%"
  3. setlocal enabledelayedexpansion
  4. set "num=255"
  5. :lp
  6. set "str=!str! "&set /a num-=1
  7. if "!str:~254,1!" neq " " goto lp
  8. echo 字符个数为%num%个&pause>nul
  9. pause>nul
复制代码

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

评分

参与人数 1PB +2 收起 理由
523066680 + 2

查看全部评分

发表于 2008-8-15 15:20:30 | 显示全部楼层
  1. ::%%a%%!ver!&^^^ ^*~                     are you o                 k ?" : \  / ` `  verfdxcweippo opj x ds gw !%%
  2. @echo off&setlocal enabledelayedexpansion
  3. set /p str=<%0
  4. for /l %%a in (1,1,255) do (set str=!str:~1!&if "!str!"=="" (set /a totle=%%a-2&echo !totle!&goto end))
  5. :end
  6. pause
复制代码
应该是111个字符吧,上楼的结果怎么是108呀?

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

评分

参与人数 2PB +16 收起 理由
pusofalse + 10
batman + 6 好,思路的确好,但效率值得考虑

查看全部评分

发表于 2008-8-15 15:44:05 | 显示全部楼层

回复 13楼 的帖子

12楼在第一次set的时候,脱掉了三个百分号。
发表于 2008-8-15 15:49:24 | 显示全部楼层
13楼的方法不错,我也这样写过,但缺点在于很难作为一个函数或者模块放到一个大的批处理里面,缺乏实用性,不知道 shqf 兄有没有好的方法?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-16 19:17 , Processed in 0.026593 second(s), 11 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表