文/523066680
blog: http://hi.baidu.com/523066680
首发于:http://www.cn-dos.net/forum/viewthread.php?tid=45654&fpage=1
写这文章的原因:
看了一些关于字符串处理的帖子,觉得方法可以改进。另外,本人的签名... 为了让更多的人看见
所以必须多发文章,而且不能尽是水。
主要内容:
1.抓了几道题目,解题,觉得见得人的就摆上来了。
2.set截取字符串另解
---------------------------------几道题------------------------------------
在群里有人问到,如何简单地判断字符串的长度?
(我确有另解,只是不通用罢了,于是问问条件) "比如说?"
"比如变量值 1 或123 我要判断有几个字符"(ok,简单短小的字符串,我包了)
测试代码1:
@echo off
set str=exist
set n=%str%9876543210
echo,%str% 字符个数为 %n:~9,1% 个
pause
可使用的情况: 字符数量在 0~9个的范围 的时候
这个方法是不通用的,但是有的问题本来就简单,何不简单地解决?
所以我提倡:特殊问题,特殊对待。
应用举例:可以判断某9位以内 数字位数;
00N式重命名的时候,判断位数并补适当位数的零
顺便贴一个 按文件个数批量00N重命名 的例子吧,思路不全同,但类似
@echo off
title code by 523066680 @ cn-dos.net
if not exist *.jpg (echo,不存在jpg文件&pause>nul&exit)
setlocal enabledelayedexpansion
::判断文件数位数,决定名字前面补0的最大个数.文件数不能多于9位数.
for %%a in (*.jpg) do set /a a+=1
set "a=%a%987654321" & set "o=0000000000"
set /a k=0
for /f "tokens=*" %%a in ('dir *.jpg /b') do (
set /a k+=1
set name=%o%!k!
ren "%%a" "0!name:~-%a:~9,1%!.jpg"
echo,0!name:~-%a:~9,1%!
)
不小心发现 包括上面和好多 BatchRen都有的一个bug ~ dir会把名字排序并列出,但是...
比如存在 0.jpg 1.jpg 第一次ren 0.jpg 为1.jpg 时...就会出错.
这里不讨论这个了.(临时对策,先ren *.jpg *.jpg# 然后再for逐个ren "xx.jpg#" "编号.jpg")
(下面只贴了标题,没有链接sorry 我是单机用户,在精华帖2008看到以下内容.....)
标题: 如何检查一字符串中相同字符的个数
--------------------------------------------------------------------------------
作者: huahua0919 时间: 2008-4-12 19:13 标题: 如何检查一字符串中相同字符的个数
set string=aferefwfwerergrgreaaffwafwa
用bat怎么检查以上字符串中字符a出现的次数(可不能用字符截取功能)
重新看下,
超级郁闷。。。。。。原来最后追的帖 思路和我的一样,怎么没人回复?我觉得比上面楼的简洁哟...
下面的我也想到的...思路一样的...code
::计算某普通字符串中 t 的个数,字符串里面不能有敏感字符
@echo off
set str=This is a te,st,t t t t
set /a for=-1
for %%a in ("%str:t=" "%") do set /a for+=1
echo %for%
pause
老掉牙了...刚混混的时候被bat-zw来个测试,将一歌词本内容居中,里面都英文字符,如下
I should have known all along.
There was something wrong.
I just never read between the lines.
Then I woke up one day and found you on your way.
Leaving nothing but my heart behind.
What can I do to make it up to you.
Promises don't come easy.
But tell me if there's a way to bring you back home to stay.
Well I'd promises anything to you.
I've been walkin' around with my head hanging down.
Wondrin' what I'm gonna do.
'Cause when you walked out that door.
保存为 lyric.txt
想了好久... 想到玩特效的时候用的一些把戏,用得上!
::只支持纯单字节字符的字符串。
@echo off &setlocal enabledelayedexpansion
for /l %%a in (1,1,80) do (set space= !space!)
for /f "tokens=* eol=" %%a in (lyric.txt) do (
set "an=%%a%space%"
set an=!an:~0,80!
set kong=!an:%%a=!
set kong=!kong: = !
echo !kong!%%a
)
pause
思路分析下,假设 单行字符个数 在80以内,往后补80空格,再抓前80个,排去字符串,剩下后面空格,
(字符串与后补空格又区分开来)
将空格两个变成一个,然后移到字符串前面,就居中拉......(咔咔,联想到俺 中间变大变小+同步变色的方框.bat)
通用的....感觉找vbs更适合
看看上面也才抓了3道题,感觉好空虚...
------以下是set截取字符串另解------仅供参考,感觉思路被混淆者请绕开-----
关于截取字符,help的解释,我困惑了很久(现在还勉强理解),于是决定尝试--用另一种方式描述
set 截取变量所产生的结果. (把保留,剔除,正负数的结果区分开来)
(我很早就在群发过以下内容,如果在别的帖子见过,怀疑我,没关系,我是一个提倡者.)
====================以下以另一种方式描述,仅供参考====================
对于 %str:~n,m%
~代表省略
后面的那个数字代表: 省略后,剩下的字符串被保留的个数。若值为负则需计算.
不管是省略还是保留,指的都是前面的字符:
-----规律------%a:~n,m% ,
-----规律------%a:~-n,-m% 剩下 n 个
这样说仍然是不易于想像的,以下给出例子:
- - - -例子:set a=123456789 共9个字符- - - - - 若出现-n的情况,则实际操作数 = 现有字符数-n
1. echo,%a:~0,5% (前面不省略) 保留前 5个
2. echo,%a:~0,-5% (前面不省略) 保留前 4个 (9-5=4)
3. echo,%a:~5% 省略前5个,剩下的保留
4. echo,%a:~-5% 省略前 4 个,剩下的保留 (剩下后5个)
5. echo,%a:~-3,2% 省略前 6 个(剩后3个:789) 剩下的再保留前2个
6. echo,%a:~-5,-3% 省略前 4 个(剩后5个:56789) ,剩下的再保留前5-3=2个, 结果显示56
7. echo %a:~1,1% 这个不用说......
- - - - - - - - - - - - - - - - - - - - - - - -
一般的说,为了避免混淆,习惯上会倾向于只使用某种形式,像我刚入门的时候尽量避免 - 号
先在则是常用了. 看上面 4. 的注释 因为省略前 总数-n 个,所以剩下的就是n个,是后面的n个.
所以 ~-n 可以方便的理解为: 保留后面n个
所以如果我要获得%time%的最后两个数字,就用%time:~-2%
而不是算一下%time%一共有几个符号,然后 %time:~9%
~-n 的优点体现在, 可以轻松获得 未知长度字符串的后n位字符.
在一些情况下适当的使用,就不用 计算字符串字符个数 啦。
------------------------------仅供参考-----------------------------
这里基本用统一方式描述了不同处理结果.尽管个别例子可用更简单的方式描述,但统一一些更重要,
所以上面的例子统一用一种规则描述,思考起来不易混淆。
----------------------------
练习也是重要的,随便说几个吧
set str=未知字符串大概十几个字符
1.获取该未知字符串的后3个字符
2.一句echo实现--显示该未知字符串的后3个字符的前两个
3.一句echo实现--显示该未知字符串的结果
算了,手痒,第3道,echo,%str:~5,-5%
可以试用上面的计算法则分析
Last edited by 523066680 on 2009-1-14 at 09:58 ]
Article by 523066680
blog: http://hi.baidu.com/523066680
Originally published at:http://www.cn-dos.net/forum/viewthread.php?tid=45654&fpage=1
Reason for writing this article:
After reading some posts about string processing, I think the methods can be improved. Also, my signature... To let more people see it, so I must post more articles and not just be idle.
Main content:
1. Grabbed a few questions, solved them, and put the ones that can be shown up.
2. Another solution for using set to intercept strings
---------------------------------Several Questions------------------------------------
Someone asked in the group how to simply judge the length of a string?
(I do have another solution, but it's not universal, so I ask about the conditions) "For example?"
"For example, if the variable value is 1 or 123, I want to judge how many characters there are" (ok, for simple short strings, I can handle it)
Test code 1:
@echo off
set str=exist
set n=%str%9876543210
echo,%str% has %n:~9,1% characters
pause
Applicable situation: When the number of characters is in the range of 0~9
This method is not universal, but some problems are originally simple, why not solve them simply?
So I advocate: Special problems, special treatment.
Application examples: Can judge the number of digits of a number within 9 digits;
When renaming in 00N format, judge the number of digits and supplement the appropriate number of zeros
By the way, post an example of batch renaming in 00N format according to the number of files, the idea is not exactly the same, but similar
@echo off
title code by 523066680 @ cn-dos.net
if not exist *.jpg (echo,No jpg files exist&pause>nul&exit)
setlocal enabledelayedexpansion
::Judge the number of digits of the file count to determine the maximum number of zeros to supplement in front of the name. The number of files cannot be more than 9 digits.
for %%a in (*.jpg) do set /a a+=1
set "a=%a%987654321" & set "o=0000000000"
set /a k=0
for /f "tokens=*" %%a in ('dir *.jpg /b') do (
set /a k+=1
set name=%o%!k!
ren "%%a" "0!name:~-%a:~9,1%!.jpg"
echo,0!name:~-%a:~9,1%!
)
Accidentally found that there is a bug including the above and many BatchRen... dir will sort and list the names, but...
For example, if there are 0.jpg 1.jpg, when renaming 0.jpg to 1.jpg for the first time... it will go wrong.
Let's not discuss this here. (Temporary countermeasure, first ren *.jpg *.jpg# and then for each ren "xx.jpg#" "number.jpg")
(Only the title is posted below, no link sorry I am an offline user, saw the following content in the essence post 2008.....)
Title: How to check the number of identical characters in a string
--------------------------------------------------------------------------------
Author: huahua0919 Time: 2008-4-12 19:13 Title: How to check the number of identical characters in a string
set string=aferefwfwerergrgreaaffwafwa
How to use bat to check the number of times character a appears in the above string (cannot use character intercepting function)
Look again,
Super depressed... The original post that was chased at the end has the same idea as mine, why no one replied? I think it's more concise than the previous building...
The following is also what I thought... the idea is the same... code
::Calculate the number of t in a common string, there should be no sensitive characters in the string
@echo off
set str=This is a te,st,t t t t
set /a for=-1
for %%a in ("%str:t=" "%") do set /a for+=1
echo %for%
pause
It's old... When I was just mixing around, I was tested by bat-zw, to center the content of a lyric book, all are English characters, as follows
I should have known all along.
There was something wrong.
I just never read between the lines.
Then I woke up one day and found you on your way.
Leaving nothing but my heart behind.
What can I do to make it up to you.
Promises don't come easy.
But tell me if there's a way to bring you back home to stay.
Well I'd promises anything to you.
I've been walkin' around with my head hanging down.
Wondrin' what I'm gonna do.
'Cause when you walked out that door.
Save as lyric.txt
Thought about it for a long time... I thought of some tricks used when playing special effects, which can be used!
::Only supports strings with pure single-byte characters.
@echo off &setlocal enabledelayedexpansion
for /l %%a in (1,1,80) do (set space= !space!)
for /f "tokens=* eol=" %%a in (lyric.txt) do (
set "an=%%a%space%"
set an=!an:~0,80!
set kong=!an:%%a=!
set kong=!kong: = !
echo !kong!%%a
)
pause
Idea analysis, assuming that the number of characters in a single line is within 80, supplement 80 spaces later, then grab the first 80, remove the string, leave the following spaces,
(The string and the following spaces are distinguished from each other)
Change two spaces into one, then move it to the front of the string, and it will be centered... (Ka ka,联想到 my "middle larger smaller + synchronous color changing box.bat")
General... I feel that finding VBS is more suitable
Looking at the above, I only grabbed 3 questions, and I feel very empty...
------The following is another solution for set intercepting strings------For reference only, those who feel that the idea is confused please bypass-----
Regarding intercepting characters, the explanation in help confused me for a long time (now I can barely understand), so I decided to try to describe it in another way
set the result generated by intercepting variables. (Distinguish the results of retention, elimination, positive and negative numbers)
(I have been sending the following content in groups for a long time. If you have seen it in other posts, suspect me, it doesn't matter, I am a promoter.)
====================The following is described in another way, for reference only====================
For %str:~n,m%
~ represents omitting
The following number represents: after omitting, the number of characters in the remaining string is retained. If the value is negative, it needs to be calculated.
Whether it is omitting or retaining, it refers to the characters in front:
-----Rule------%a:~n,m% ,
-----Rule------%a:~-n,-m% and then retain the first n-m characters of the remaining n characters
This statement is still not easy to imagine. The following examples are given:
- - - -Example: set a=123456789 with 9 characters- - - - - If the situation of -n occurs, the actual operation number = existing characters -n
1. echo,%a:~0,5% (no omission in front) retain the first 5
2. echo,%a:~0,-5% (no omission in front) retain the first 4 (9-5=4)
3. echo,%a:~5% omit the first 5 characters, retain the remaining
4. echo,%a:~-5% omit the first 4 characters, retain the remaining, the result is the last 5 characters
5. echo,%a:~-3,2% omit the first 6 characters (remaining last 3:789) and then retain the first 2 characters of the remaining
6. echo,%a:~-5,-3% omit the first 4 characters (remaining last 5:56789), then retain the first 5-3=2 characters of the remaining, the result is 56
7. echo %a:~1,1% This doesn't need to be said......
- - - - - - - - - - - - - - - - - - - - - - - -
Generally speaking, in order to avoid confusion, people tend to only use a certain form. When I just started, I tried to avoid the - sign
Now it is commonly used. Look at the comment 4 above. Because the first total-n characters are omitted, the remaining are the last n characters.
So ~-n can be easily understood as: retain the last n characters
So if I want to get the last two digits of %time%, I use %time:~-2%
Instead of calculating how many symbols %time% has, then %time:~9%
The advantage of ~-n is reflected in that it can easily obtain the last n characters of a string of unknown length.
In some cases, use it appropriately, and you don't need to calculate the number of characters in the string.
------------------------------For reference only-----------------------------
Here, it is basically described in a unified way for different processing results. Although some examples can be described in a simpler way, unifying some is more important,
So the above examples are described in a unified rule, which is not easy to confuse when thinking.
----------------------------
Practice is also important, just say a few
set str=Unknown string with probably more than a dozen characters
1. Get the last 3 characters of this unknown string
2. One echo to achieve--display the first two of the last 3 characters of this unknown string
3. One echo to achieve--display the result of of this unknown string
Forget it, itchy hands, the third question, echo,%str:~5,-5%
You can use the above calculation rules to analyze
Last edited by 523066680 on 2009-1-14 at 09:58 ]