Windows BAT batch string related operations (string definition, segmentation, splicing, replacement, slicing, search)

1. String definition

Use setto define strings

@echo off
set string1=Hello
echo %string1%

Code description:

  1. Assign the string Hello to the variable string1

2. String concatenation

Use %string1%%string2%the way to complete the splicing of strings.

@echo off
set string1=Hello
set string2=World
set combined1=%string1% %string2%
set combined2=%string1%,%string2%
set combined3=%string1%%string2%
set combined4=aaa%string1%bbb%string2%ccc
set "combined5=%string1%^&%string2%"
echo %combined1%
echo %combined2%
echo %combined3%
echo %combined4%
echo %combined5%

Code description:

  1. ^The string used to concatenate strings needs to be escaped and enclosed if it contains special characters "". Special characters include (but are not limited to):
symbol effect
@ command line echo mask
% batch variable guide
> Redirector
>> Redirector
<>&<& Redirector
| command pipe character
^ escape character
& Combination command
&& Combination command
|| Combination command
"" string delimiter

3. String segmentation

A string My-String, and you want to split it into My and String.

@echo off
set my_string=My-String
for /f "tokens=1,2 delims=-" %%a in ("%my_string%") do (
   set first_part=%%a
   set second_part=%%b
)
echo First part: %first_part%
echo Second part: %second_part%

Code description:

  1. A variable called my_string is defined with a value of My-String
  2. It uses for /fthe command to split the string. tokens=1,2The split part (tokens) is specified, and delims=-the delimiters (delimiters) are specified
  3. The split parts are assigned to %%aand %%bthen stored in the first_part and second_part variables

In the above example, delims=-the character set is specified as the separator, for example, it delims=+-$#means that the character string is + - & #divided separately.
Sometimes a string may be needed as a separator, which can be achieved in the following ways:

@echo off
set cmd_str=REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "Common Desktop"
for /F "skip=2 tokens=*" %%S in ('%cmd_str%') do set str=%%S
::set "str=Common Desktop    REG_EXPAND_SZ    %%PUBLIC%%\Desktop"

:: 利用字符串替换功能,将    REG_EXPAND_SZ    替换为#,注意这里两端各有4个空格
set str=%str:    REG_EXPAND_SZ    =#%

:: 然后再利用for /f函数,delims=# 来分割字符串
for /F "tokens=1,2 delims=#" %%a in ("%str%") do (
	set value_name=%%a
	set value=%%b
)
echo value_name="%value_name%"
echo value="%value%"

Code description:

  1. Use the string replacement function to replace multiple characters REG_EXPAND_SZ with a single character #. Note that there are 4 spaces at both ends;
  2. : Then use for /fthe method delims=#to split the string, so as to achieve the purpose of saving the country with a curve

4. String replacement

Syntax: %var:old_str=new_str%
Description: Use str2 to replace part of str1 in the var variable

@echo off
set VAR=hello
set VAR=%VAR:ell=ipp%
echo %VAR%

Code description:

  1. A variable VAR is created with the value "hello". Then, replace "ell" with "ipp". Therefore, the script will output "hippo".

5. String Slicing

Syntax: 目标字符串=%源字符串:~起始位置,截取长度%
Description: start_index is the starting position of the substring, length is the length of the substring. Indexes are 0-based, so 0 represents the first character of the string.

@echo off
echo %%date%%=%date%
echo %%date:~0,4%%=%date:~0,4%
echo %%date:~5,2%%=%date:~5,2%
echo %%date:~8,2%%=%date:~8,2%
echo %%date:~-2%%=%date:~-2%
echo %%date:~0,-2%%=%date:~0,-2%
echo %%date:~-8,3%%=%date:~-8,3%
echo %%date:~-8,-3%%=%date:~-8,-3%
echo %%date:~4%%=%date:~4%
echo %%date:~,4%%=%date:~,4%
echo %%date:~4,%%=%date:~4,%

output:

%date%=2023/07/11 周二
%date:~0,4%=2023
%date:~5,2%=07
%date:~8,2%=11
%date:~-2%=周二
%date:~0,-2%=2023/07/11
%date:~-8,3%=07/
%date:~-8,-3%=07/11
%date:~4%=/07/11 周二
%date:~,4%=2023
%date:~4,%=

Code description:

  • Starting position:
    1) The starting value is positive, indicating a positive number, such as: %date:~5,2%, indicating that 2 characters are intercepted from the fifth character of the positive number (excluding the fifth character);
    2) The starting value If it is negative, it means counting backwards, such as: %date:~-8,3%, which means to intercept 3 characters (including the 8th character) from the 8th character backwards;
    3) The starting value is 0 or empty, which means starting from the leftmost , such as: %date:~,4%, %date:~0,4%both mean to intercept 2 characters from the starting position on the left;
    (if the starting position is omitted, there must be a comma placeholder, if the comma is omitted, it means [cutoff length is empty])
  • Cut-off length (offset):
    1) The cut-off length is positive, indicating the interception length, such as: %date:~8,2%, which means to intercept 2 characters from the 8th character of the positive number (excluding the 8th character);
    2) Cut-off The length is negative, indicating the end position, such as: %date:~-8,-3%, indicating that the 8th character from the bottom is intercepted backwards, and has been intercepted to the position of the 3rd from the last character (including the 8th from the bottom, excluding the 3rd from the bottom) );
    3) If the cut-off length is empty, it means to intercept to the end position, such as: , %date:~4%which means to intercept all characters from the 4th character of the positive number (excluding the 4th character);
    (If the cut-off length is omitted, there cannot be A comma takes place, otherwise the value is empty)

6. String lookup

Use the echoand findstrcommand to determine whether a string is contained

@echo off
set VAR=hello
echo %VAR% | findstr /C:"ell" >nul
if errorlevel 1 (
    echo String not found
) else (
    echo String found
)

Code description:

  1. A variable VAR is created with the value "hello". Then, use the echo and findstr commands to find "ell". If "ell" is found, the script will output "String found", otherwise, output "String not found"

Guess you like

Origin blog.csdn.net/B11050729/article/details/131655470