Batch Script – Left String
In this article, we are going to study Left string in Batch Script. In Batch Script, the Left string is used to extract the characters from the beginning of the string by giving position 0 and a length using:~ while expanding a variable content.
Example 1:
Batch script
set str=w3wiki echo.%str% set str=%str:~0,5% echo.%str%
Output
w3wiki Beginner
In the above example, the key point you notice is :~0,5, which means that we need to display the characters starting from the position from 0 to 5.
Example 2:
Batch Script
set str=Hello World echo.%str% set str=%str:~0,4% echo.%str%
Output
Hello World Hell
Using the Cut command
Cut command can also be used to extract the characters from the string that takes a few flags ( -cN-M ) as input and output the required substring. When you provide both variables i.e string and flag then it will return to you the characters in the string starting from index N and ending at M with both indexes included.
Given below is an example of how to use the Cut command in Bash
Batch script
$ echo "w3wiki" | cut -c0-6
Output
BeginnerF
Contact Us