2024年6月30日 星期日

Batch Script - Strings

 

Example

@echo off 
:: This program just displays Hello World 
set message = Hello World 
echo %message%

Output

The above command produces the following output.

Hello World 

 

 

 

 

 

An empty string can be created in DOS Scripting by assigning it no value during it’s initialization as shown in the following example.

Set a=

To check for an existence of an empty string, you need to encompass the variable name in square brackets and also compare it against a value in square brackets as shown in the following example.

[%a%]==[]

The following example shows how an empty string can be created and how to check for the existence of an empty string.

Example

@echo off 
SET a= 
SET b=Hello 
if [%a%]==[] echo "String A is empty" 
if [%b%]==[] echo "String B is empty "
 

Output

The above command produces the following output.

String A is empty 

 

 

 

 

 

String interpolation is a way to construct a new String value from a mix of constants, variables, literals, and expressions by including their values inside a string literal.

In DOS scripting, the string interpolation can be done using the set command and lining up the numeric defined variables or any other literals in one line when using the set command.

The following example shows how a string interpolation can be done with numeric values as well.

Example

@echo off 
SET a = Hello 
SET b = World 
SET /A d = 50 
SET c=%a% and %b% %d%
echo %c%
 

Output

The above command produces the following output.

Hello and World 50 

 

 

 

 

 

You can use the set operator to concatenate two strings or a string and a character, or two characters. Following is a simple example which shows how to use string concatenation.

Example

@echo off 
SET a = Hello 
SET b = World 
SET c=%a% and %b% 
echo %c%

Output

The above command produces the following output.

Hello and World 
 
 
 
 
 

In DOS scripting, there is no length function defined for finding the length of a string. There are custom-defined functions which can be used for the same. Following is an example of a custom-defined function for seeing the length of a string.

Example

@echo off set str = Hello World call :strLen str strlen echo String is %strlen% characters long exit /b :strLen setlocal enabledelayedexpansion :strLen_Loop if not "!%1:~%len%!"=="" set /A len+=1 & goto :strLen_Loop (endlocal & set %2=%len%) goto :eof

A few key things to keep in mind about the above program are −

  • The actual code which finds the length of string is defined in the :strLen block.

  • The length of the string is maintained in the variable len.


    Output

    The above command produces the following output.

    11 
     
     
     
     
     
     

    Example

    @echo off set var = 13145 set /A var=%var% + 5 echo %var%

    Output

    The above command produces the following output.

    13150

    Apart from this, strings have the following implementations which are available. Batch scripts have the following commands which are used to carry out string manipulation in strings.

    %variable:~num_chars_to_skip% %variable:~num_chars_to_skip,num_chars_to_keep%

    This can include negative numbers −

    %variable:~num_chars_to_skip, -num_chars_to_keep% %variable:~-num_chars_to_skip,num_chars_to_keep% %variable:~-num_chars_to_skip,-num_chars_to_keep%

    Let us discuss the possible string operations that can be performed by using the above commands.

     

     

     

     

     

    Example

    @echo off 
    set x = 1000 
    set y = 1 
    set y = %y% 
    echo %x% 
    
    set y = %y:~-4% 
    echo %y%

    A few key things to note about the above program is −

  • Spaces are added to the variable of y, in this case we are adding 9 spaces to the variable of y.

  • We are using the ~-4 option to say that we just want to show the last 4 characters of the string y.

     

    Output

    The above command produces the following output. The key thing to note is that the value of 2 is aligned to match the units columns when displaying numbers.

    1000
    1 
     
     
     
     
     

    Example

    set str=Helloworld echo.%str% set str=%str:~0,5% echo.%str%

    The key thing to note about the above program is, ~0,5 is used to specify the characters which needs to be displayed. In this case, we are saying character 0 to 5 should be displayed.

    Output

    The above command produces the following output.

    Helloworld Hello 
  •  
     
     
     
     
     
     

    Example

    @echo off set str = Helloworld echo %str% set str = %str:~5,10% echo %str%

    The key thing to note about the above program is, ~5,10 is used to specify the characters which needs to be displayed. In this case, we want character 5 to 10 should be displayed.

     

    Output

    The above command produces the following output.

    Helloworld 
    world 
     
     
     
     
     

    Example

    @echo off set str = Batch scripts is easy. It is really easy. echo %str% set str = %str:is = % echo %str%

    The key thing to note about the above program is, the ‘is’ word is being removed from the string using the :’stringtoberemoved’ = command.

    Output

    The above command produces the following output.

    Batch scripts is easy. It is really easy. Batch scripts easy. It really easy.
     

     

     

     

     

    Example

    @echo off 
    set str = This string    has    a  lot  of spaces 
    echo %str% 
    
    set str=%str:=% 
    echo %str%

    The key thing to note about the above program is, the : = operator is used to remove all spaces from a string.

    Output

    The above command produces the following output.

    This string    has    a  lot  of spaces
    Thisstringhasalotofspaces
    

 

 

 

 

 

 

Example

@echo off 
set str=This message needs changed. 
echo %str% 

set str=%str:needs=has% 
echo %str%

The key thing to note about the above program is, the example replaces the word ‘needs’ with the string ‘has’ via the statement %str:needs = has%

Output

The above command produces the following output.

This message needs changed. 
This message has changed.

 

 
 
 
 
 

Example

@echo off set str = This message needs changed. echo %str% set str = %str:~-8% echo %str%

The key thing to note about the above program is, the right hand of the string is extracted by using the ~-‘number of characters to extract’ operator.

Output

The above command produces the following output.

This message needs changed. changed.
 

 

 

 

 

沒有留言: