China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-24 09:25
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Discussion] About Strings View 2,954 Replies 5
Original Poster Posted 2009-01-10 11:12 ·  中国 广东 揭阳 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
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 ]
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
HAT +8 2009-01-10 15:51
tireless +5 2009-01-10 21:34
Floor 2 Posted 2009-01-10 11:13 ·  中国 广东 揭阳 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
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!
---------------------------------------------------------------
Floor 3 Posted 2009-01-10 14:58 ·  中国 贵州 安顺 电信
初级用户
Credits 94
Posts 49
Joined 2008-12-14 20:47
17-year member
UID 133884
Gender Male
Status Offline
The special solution of can be extended to 16 characters, ^_^
@echo off
set str=exist
set n=%str%fedcba9876543210
set/a n=0x%n:~16,1%+1
echo,%str% has %n% characters
pause
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
523066680 +5 2009-01-15 14:16
Floor 4 Posted 2009-01-10 15:02 ·  中国 广东 揭阳 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
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.

@echo off
set str=cn-dos
set st2=Str%str%11111111111111111111111111111111111111111111111111111111111111111111111111111111
set "st2=%st2:~0,83%"
call set n=%%st2:Str%str%=-0%%
set /a n=80 %n:1=-1%
echo,%n%
pause
::The number of characters can be 0, and it's a common string in the range of 0~80.
Floor 5 Posted 2009-01-10 15:51 ·  中国 重庆 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
It is suggested to add some search - friendly keywords in the post title.
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
523066680 +3 2009-01-14 09:58
Floor 6 Posted 2009-01-14 10:01 ·  中国 广东 揭阳 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
Although it's a folk remedy, I still post it. 40 characters

@echo off
set str=abcde
set st2=%str%%str%4039383736353433323130292827262524232221201918171615141312111009080706050403020100
echo,%str% has %st2:~80,2% characters
pause
Forum Jump: