Thursday, January 13, 2011

Left String - Extract characters from the beginning of a string

Description:
Similar to the Left function in VB a batch script can return a specified number of characters from the left side of a string by specifying a substring for an expansion given a position of 0 and a length using :~ while expanding a variable content. The example shows how to return the first 4 characters of a string.

Script:

set str=politic
echo.%str%
set str=%str:~0,4%
echo.%str%

also

@echo off
echo. %0
set str=%0
echo.%str%
set str=%str:~0,-10%
set str=%str:~1,-1%
echo.%str%
pause

No comments:

Post a Comment