:: Operating system: Windows XP SP3
@echo off
:: Initialize variables
Set Str1=this is string1
Set Str2=this is string2
Set Str3=this is String3
:: First print out the original data
Echo str1=%str1%
Echo str2=%str2%
Echo str3=%str3%
:: Similar to strcpy, copy a string to another character pointer or array of characters, overwriting the original string
:: Implementation method: Set Target string =% source string%
Echo.
Echo-----------------------------------------------
Set strcpy=%str1%
Echo strcpy=%strcpy%
:: Similar to Strcat, concatenate one string to the end of another character pointer or character array.
:: Implementation method: Set target string =% target string% source string%
Echo.
Echo-----------------------------------------------
Set strcat=%str1%%str2%
Echo strcat=%strcat%
:: String truncation, no such function in C.
:: Implementation method: Set Target string =% source string: ~ Start value, intercept length%
:: Note that the starting value starts at 0!
:: The Intercept length is optional, and if you omit the comma and intercept the length, it will be truncated from the starting value to the end of the string.
Echo.
Echo-----------------------------------------------
Set strinterception1=%str1:~0,4%
Set strinterception2=%str1:~1,4%
Set strinterception3=%str1:~5%
Echo strinterception1=%strinterception1%
Echo strinterception2=%strinterception2%
Echo strinterception3=%strinterception3%
:: Similar to strlen, gets the length of the string.
:: Implementation method: Using Goto and tags to form a looping structure, the string is continuously truncated 1 characters, and the number of truncation of the variable record, until the string into an empty string
Echo.
Echo-----------------------------------------------
Set str=%str1%
: Next
If not "%str%" = = "" (
set/a num+=1
Set "str=%str:~1%"
Goto Next
)
The length of the echo "%str1%" string is:%num%
PAUSE
Windows bat String manipulation