![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-11 06:02 |
47,811 topics / 349,897 posts / today 0 new / 48,256 members |
| DOS批处理 & 脚本技术(批处理室) » [Discussion] About Strings |
| Printable Version 3,060 / 5 |
| Floor1 523066680 | Posted 2009-01-10 11:12 |
| 银牌会员 Posts 1,133 Credits 2,362 | |
|
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: 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 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 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
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! 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 ] |
|
| Floor2 523066680 | Posted 2009-01-10 11:13 |
| 银牌会员 Posts 1,133 Credits 2,362 | |
|
I flipped through the essence posts again, and it turns out I missed so many good things. By the way, I'll post the title links....
------------------------------------------------------------- How to judge the length of a variable?? Original link: http://www.cn-dos.net/forum/viewthread.php?tid=24745 --------------------------------------------------------------- How to get the number of characters and intercept characters Original link: http://www.cn-dos.net/forum/viewthread.php?tid=28571 --------------------------------------------------------------- [Wonderful][Batch processing character occurrence count] Original link: http://www.cn-dos.net/forum/viewthread.php?tid=25182 A similar one How to count the number of times a certain string appears in a text? Original link: http://www.cn-dos.net/forum/viewthread.php?tid=21250 --------------------------------------------------------------- Another post caught: Title: How to verify that the input string contains double-byte characters (Saw it in the essence post 2008, I'm a single-user, didn't catch the link) --------------------------------------------------------------- Title: Optimize the code for calculating string length, thank you! --------------------------------------------------------------- |
|
| Floor3 linee | Posted 2009-01-10 14:58 |
| 初级用户 Posts 49 Credits 94 | |
|
The special solution of can be extended to 16 characters, ^_^
|
|
| Floor4 523066680 | Posted 2009-01-10 15:02 |
| 银牌会员 Posts 1,133 Credits 2,362 | |
|
It's a pity I can't add points anymore. I've done other extensions, but just the sentence became longer. Because it's not universal, is there any good thing from other predecessors, so I didn't post it.
|
|
| Floor5 HAT | Posted 2009-01-10 15:51 |
| 版主 Posts 5,017 Credits 9,023 | |
|
It is suggested to add some search - friendly keywords in the post title.
|
|
| Floor6 523066680 | Posted 2009-01-14 10:01 |
| 银牌会员 Posts 1,133 Credits 2,362 | |
|
Although it's a folk remedy, I still post it. 40 characters
|
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |