[Share] methods for processing strings in bat

Source: Internet
Author: User
The C language contains various string processing functions. bat is not as powerful as the C language, so it can only be combined with the set statement for some simple string processing.
 
The format of the SET command is as follows:
 
Environment variable replacement has been enhanced as follows :,
 
% PATH: str1 = str2 %
 
The path environment variable is extended, and "str2" is used to replace each "str1" in the extended result ".
 
To effectively delete all "str1" and "str2" from the extension results, it can be empty.
 
"Str1" can start with a star. In this case, "str1" will expand the result from
 
From the beginning to the place where the rest of str1 appears for the first time, they always match.
 
The extension can also be a substring.
 
% PATH :~ 10, 5%
 
The path environment variables will be extended, and then only 11th (partial
 
10 characters. If no length is specified, the default value is used.
 
Value, that is, the remainder of the variable value. If both numbers (offset and length) are negative,
 
The number used is the length of the environmental variable value plus the specified offset or length.
 
% PATH :~ -10%
 
Extracts the last 10 characters of the PATH variable.
 
% PATH :~ 0,-2%.
 
All characters in the PATH variable, except the last two.
 
The specific format can be set /? Query, or set/?> Set.txt export to TXT file
 
In fact, the most common string processing method is to combine the above several usage methods,
 
1. The strcpy function in the C language copies a string to another struct pointer or character array to overwrite the original string.
 
Method of calling in C language: strcpy (target string, source string)
 
Implementation Methods in batch processing:
 
Set target string = % source string %
 
Example:
 
Copy content to clipboard
Code:
 
@ Echo off
: Disable screen echo (optional)
Set str1 = This is old string
: Set the string stored in str1. Note that there are no double quotation marks. This is different from C!
Set str2 = This is New String
: Set the string stored in str2
Echo:
Echo str1 = % str1 %
Echo str2 = % str2 %
: First output the original string
Set str1 = % str2 %
: String Copy
After echo executes string copy:
Echo str1 = % str1 %
Echo str2 = % str2 %
: Output the string after the string is copied.
After the echo output is complete, press any key to Exit & pause> NUL & Exit
: Output information. When you press any key, the batch processing is completed.
 
2. The strcat function in C connects a string to the end of another character pointer or character array.
 
Method of calling in C language: strcat (target string, source string)
 
Implementation Methods in batch processing:
 
Set target string = % target string % source string %
 
Example:
Copy content to clipboard
Code:
 
@ Echo off
Set str1 = This is string1
Set str2 = This is string2
: Set strings stored in str1 and str2
Before the echo connection string:
Echo str1 = % str1 %
Echo str2 = % str2 %
: First output the original string
Set str1 = % str1 % str2 %
: String connection
After the echo connection string:
Echo str1 = % str1 %
Echo str2 = % str2 %
: Output the two strings after the string connection is executed.
After the echo output is complete, press any key to Exit & pause> NUL & Exit
 
 
3. String truncation. This function is not available in C, but it can be implemented through statements. It is not introduced. It is called batch processing.
 
Set target string = % source string :~ Starting value, truncation length %
 
Note that the starting value starts from 0!
The Truncation length is optional. If commas (,) and the truncation length are omitted, the truncation length is truncated from the starting value to the end of the string.
 
Example:
 
Copy content to clipboard
Code:
 
@ Echo off
Set str1 = This is string1
: Set the string stored in str1
Set str2 = % str1 :~ 8, 6%
Set str3 = % str1 :~ 0, 4%
Set str4 = % str1 :~ 5%
: String Truncation
Echo original string:
Echo str1 = % str1 %
String intercepted by ECHO:
Echo str2 = % str2 %
Echo str3 = % str3 %
Echo str4 = % str4 %
: Output the execution result
After the echo output is complete, press any key to Exit & pause> NUL & Exit
4. Use the strlen function in C to obtain the string length.
 
C language call method: strlen (string)
 
In batch processing, the implementation method is to use the Goto and tag to form a loop structure. The string is truncated by 1 character continuously, and the number of truncated times is recorded with the variable until the string becomes an empty string.
 
Example:
 
Copy content to clipboard
Code:
 
@ Echo off
Set str1 = This is a test string
Set str2 = Hello World
: Set two strings
Set STR = % str1 %
: Copy str1 to Str
: Next1
: Tag, used for goto jump
: Note the difference with the comment statement. The comment starts with two colons, And the label is a colon.
If not "% STR %" = ""(
: Judge whether STR is a Null String. If not, execute the following statement.
Set/a num + = 1
: Arithmetic operation, so that the num value increases by 1, equivalent to the num ++ or ++ num statement
Set "str = % STR :~ 1%"
: Truncates a string and assigns it to itself.
Goto next1
: Jump to the next1 tag
: Here, the Goto and tag are used to form a loop structure.
)
: When the preceding loop structure is executed, the following statement is executed.
Echo str1 = % str1 %
Echo str1 length: % num %
: Output result
Set num = 0
: Set num, the environment variable for recording and using, to 0 to start the next operation.
Set STR = % str2 %
: Copy str2 to Str
: Next2
: Define a new tag
: Note that it cannot be the same as an existing tag. Otherwise, an error will occur!
If not "% STR %" = ""(
Set/a num + = 1
Set "str = % STR :~ 1%"
Goto next2
)
: Similar to the previous loop.
Echo str2 = % str2 %
Echo str2 length: % num %
: Output result
After the echo output is complete, press any key to Exit & pause> NUL & Exit
 
5. The strchr function in C finds the first occurrence position of a character in a string, returns the position when it finds it, and returns 0 when it cannot find it.
 
Train of Thought in batch processing: Constantly truncate the string and take the first character in the truncated string. Compare it with the required character. If it is the same, use the GOTO statement to jump out of the loop and output the result, if there are no identical characters, 0 is output after execution.
 
Example:
 
Copy content to clipboard
Code:
 
@ Echo off
Setlocal enabledelayedexpansion
: To enable command extension, add setlocal /? Command
Set str1 = This is a test string
Set bandwidth = T
: Note: It is case sensitive!
Set STR = % str1 %
: Copy the string to be truncated without affecting the source string.
: Next
If not "% STR %" = ""(
Set/a num + = 1
If "! STR :~ 0, 1! "=" % Failed % "Goto last
: Checks whether the first character is a required character. If yes, the system jumps out of the loop.
Set "str = % STR :~ 1%"
Goto next
)
Set/a num = 0
: If no character is found, set num to zero.
: Last
The first appearance position of ECHO character '% character %' in string "% str1 %" is % num %
After the echo output is complete, press any key to Exit & pause> NUL & Exit
 
 
 

[Share] methods for processing strings in bat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.