::操作系統:windows xp sp3
@echo off
::初始化變量
set str1=This is string1
set str2=This is string2
set str3=This is string3
::先打印出原始數據
echo str1=%str1%
echo str2=%str2%
echo str3=%str3%
::類似strcpy,將一個字符串復制到另一個字符型指針或字符數組,覆蓋原來的字符串
::實現方法:set 目標字符串=%源字符串%
echo.
echo -----------------------------------------------
set strcpy=%str1%
echo strcpy=%strcpy%
::類似strcat,將一個字符串連接到另一個字符型指針或字符數組的末尾。
::實現方法:set 目標字符串=%目標字符串%%源字符串%
echo.
echo -----------------------------------------------
set strcat=%str1%%str2%
echo strcat=%strcat%
::字符串截取,C中沒有這種函數。
::實現方法:set 目標字符串=%源字符串:~起始值,截取長度%
::注意,起始值從0開始!
::截取長度是可選的,如果省略逗號和截取長度,將會從起始值一直截取到字符串的結尾。
echo.
echo -----------------------------------------------
set strInterception1=%str1:~0,4%
set strInterception2=%str1:~1,4%
set strInterception3=%str1:~5%
echo strInterception1=%strInterception1%
echo strInterception2=%strInterception2%
echo strInterception3=%strInterception3%
::類似strlen,取得字符串的長度。
::實現方法:利用goto和標簽,形成循環結構,不斷將字符串截短1字符,並用變量記錄截短的次數,直到字符串變成空串
echo.
echo -----------------------------------------------
set str=%str1%
:next
if not "%str%"=="" (
set /a num+=1
set "str=%str:~1%"
goto next
)
echo "%str1%"字符串的長度為:%num%
PAUSE