Open In App

Batch Script – Left String

Improve
Improve
Like Article
Like
Save
Share
Report

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=GeeksForGeeks
echo.%str%
set str=%str:~0,5%
echo.%str%

Output 

GeeksForGeeks
Geeks

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 "GeeksForGeeks" | cut -c0-6

Output

GeeksF

Last Updated : 19 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads