奇怪的结果使用脚本来查找字符串长度? [英] Wierd Results using script to find length of a string?

查看:116
本文介绍了奇怪的结果使用脚本来查找字符串长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是测试在<一个提交unclemeat此code href=\"http://stackoverflow.com/questions/21817684/batch-get-string-length-with-special-characters\">this问题(UncleMeat参考这网站),当我通过输入一些carots测试它( ^ ),它产生了一些有趣的结果。

I was testing this code submitted by unclemeat in this question(UncleMeat referenced this site) when I tested it by inputting some carots(^) it produced some interesting results.

@echo off
setLocal EnableDelayedExpansion

set s=%1
set length=0

:count
if defined s (
    set s=%s:~1%
    set /A length += 1
    goto count
)

echo %length%

Len.bat的测试

C:\Users\Public>len ^
More?
More?
0

C:\Users\Public>len ^^
12

C:\Users\Public>len ^^^
More?
More?
12

C:\Users\Public>len ^^^^
1

C:\Users\Public>len ^^^^^
More?
More?
1

C:\Users\Public>len ^^^^^^
13

C:\Users\Public>len ^^^^^^^
More?
More?
13

C:\Users\Public>len ^^^^^^^^
22

C:\Users\Public>

忽略双更多,我只是没有输入任何内容,返回,该模式是:

Ignoring the double More? where I simply returned without inputting anything, the pattern is:


  1. 0

  2. 12

  3. 12

  4. 1

  5. 1

  6. 13

  7. 13

  8. 22

  9. 22

  10. 13

  11. 13

  12. 2

  13. 2

  14. 14

  15. 14

  16. 23

  17. 23

  18. 14

  19. 14

  20. 23

  21. 23

  22. 14

  23. 14

  24. 23

  25. 23

,每一个奇数次数提示我有双更多?,这就是为什么它是一倍,但其他明智的这些结果只是奇怪。我以为它会做一些与在code以下行,但似乎没有关系!

Every odd occurance prompts me with the double More?, which is why it is doubled, but other wise these results are just wierd. I thought it would have to do something with the following line in the code, but there seems to be no relationship!

任何解释这种不规则的数据?或者是这些东西约CMD这只是一个....

Any explanation to this irregular data? Or is this just one of those things about cmd....

推荐答案

它为什么code完全地失败,脱字的原因有很多。结果
首先你的方式尝试调用批处理将失败。

It has many reasons why the code completly fails with carets.
First the way you try to call your batch will fail.

一个插入符转义下一个字符,本身从线路中删除。结果
在线路末端单尖脱线端(这就是所谓的多行插入符号),这就是为什么原因CMD.EXE告诉你的提示更多?
这将是适用于所有奇数脱字的。

A caret escapes the next character and is itself removed from the line.
A single caret at a line end escapes the line end (it's called multiline caret), that's the cause why cmd.exe show you the prompt More?. This will be true for all odd number of carets.

样品有七个插入符号。

length ^^^^^^^
More?
More?

CMD.EXE将调用长度蝙蝠以下字符串 ^ ^ ^&LT;换行方式&gt; 结果
新行会从%1 参数%1被分割,所以只有 ^ ^ ^

cmd.exe will call the length bat with the following string ^^^<newline>.
The newline will be split from the %1 parameter, so in %1 is only ^^^.

但现在你这一部分完全地失败

But now you this part fails completly

set s=%1
set length=0

作为它扩展为
    集合S = ^ ^ ^
    集长度= 0

As it expands to set s=^^^ set length=0

作为最后插入符号现在是一个多行插入符它将追加 设定长度= 0 来就行了!

As the last caret is now a multiline caret it will append set length=0 to the line!

因此​​,在变量取值现在内容 ^设定的长度= 0 。结果
这不会工作...

So in the variable s is now the content ^set length=0.
This will never work ...

即使在此块中的%S:〜1%将是进一步的问题的原因,因为它也将可以扩展到多行插入符号时取值插入符号包含(当使用8插入符长度^^^^^^^^ )。

Even in this block the %s:~1% will be a cause of further problems, as it will also can expand to multiline carets when s contains carets (when you use 8 carets length ^^^^^^^^).

if defined s (
    set s=%s:~1%
    set /A length += 1
    goto count
)

有关插入符号的一些更多的解释,你可以阅读
SO:长命令拆分为多行Vista中/ DOS批处理文件(.bat)

For some more explanations about the caret you can read SO:Long commands split over multiple lines in Vista/DOS batch (.bat) file

这篇关于奇怪的结果使用脚本来查找字符串长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆