雖然批處理個人用的不多,但往往需要用一次時,發現很多見的東東不懂,這裡做下小筆記:
::作業系統:windows xp sp3@echo off::初始設定變數set str1=This is string1set str2=This is string2set 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%:nextif not "%str%"=="" ( set /a num+=1 set "str=%str:~1%" goto next)echo "%str1%"字串的長度為:%num%PAUSE
運行結果如所示: