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 ]
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 ]
Recent Ratings for This Post
( 2 in total)
Click for details

