onlyforbopi
1/5/2017 - 1:25 PM

DOS.String.Manipulation

DOS.String.Manipulation #dos #DOS #string #strings #manip #manipulation #substring #slicing #index #replace #length #align

DOS_STRING_MANIPULATION

SET testa=alalalala


:: Slicing from end
echo %testa:~0,-1%
echo %testa:~0,-2%
echo %testa:~0,-3%

:: Slicing from beginning
echo %testa:~1%
echo %testa:~2%
echo %testa:~3%
1.  DOS_Create_string                   : Assign string variable
2.  DOS_SlicingSubstring                : Explains how to slice / cut substrings
3.  DOS_Check_for_empty_str             : Check for empty string
4.  DOS_string_interpolation            : Expand string vars to create other vars
5.  DOS_string_concat                   : Concatenate strings
6.  DOS_string_length                   : Find the length of a string
7.  DOS_string_align                    : Align string to right
8.  DOS_remove_substring                : Remove a substring
9.  DOS_replace_instring                : Replace a part of a string
10. DOS_slicefrombeginorend                : Replace characters from the beginning
:E. Length of String (Custom)

@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
:C. Interpolation

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

Hello and World 50
K. REMOVE SUBSTRING
@echo off 
set str = Batch scripts is easy. It is really easy. 
echo %str% 

set str = %str:is =% 
echo %str%


L. REMOVE BOTH ENDS OF SUBSTRING
@echo off 
set str = Batch scripts is easy. It is really easy 
echo %str% 

set str = %str:~1,-1% 
echo %str%


M. REMOVE ALL SPACES
@echo off 
set str = This string    has    a  lot  of spaces 
echo %str% 

set str = %str: =% 
echo %str%
:D. Concatenation

@echo off 
SET a = Hello 
SET b = World 
SET c = %a% and %b% 
echo %c%
SET FULLDATE=%btchinfoline:~7,8%
SET DDBTCHINFO=%FULLDATE:~0,2%
SET MMBTCHINFO=%FULLDATE:~2,2%
SET YYBTCHINFO=%FULLDATE:~4,4%

:: Format
:: SET NEWVAR=%OLDVAR%:~FROM,TO%


H. SUBSTRING (LEFT STRING)
@echo off 
set str = Helloworld 
echo %str% 

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

J. SUBSTRING (MID STRING)
@echo off 
set str = Helloworld 
echo %str%

set str = %str:~5,10% 
echo %str%

KS SUBSTRING (RIGHT STRING)
@echo off 
set str = This message needs changed. 
echo %str% 

set str = %str:~-8% 
echo %str%

O. REPLACE IN STRING
@echo off 
set str = This message needs changed. 
echo %str% 

set str = %str:needs = has% 
echo %str%
:B. Empty String
::B.1 Check for existence of empty string
SET A=

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

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