批處理之字串處理總結

來源:互聯網
上載者:User

雖然批處理個人用的不多,但往往需要用一次時,發現很多見的東東不懂,這裡做下小筆記:

::作業系統: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

運行結果如所示:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.