批處理中字符串分割實(shí)現(xiàn)代碼
批處理 字符串分割 實(shí)例
使用for命令可以對(duì)字符串進(jìn)行分段處理。
分割字符串
@echo off ::定義一個(gè)以分號(hào)作為分隔的字符串 set str=AAA;BBB;CCC;DDD;EEE;FFF ::str的副本 set remain=%str% :loop for /f "tokens=1* delims=;" %%a in ("%remain%") do ( ::輸出第一個(gè)分段(令牌) echo %%a rem 將截取剩下的部分賦給變量remain,其實(shí)這里可以使用延遲變量開關(guān) set remain=%%b ) ::如果還有剩余,則繼續(xù)分割 if defined remain goto :loop pause
主要解釋for語句:
delims=;表示以分號(hào)作為分隔符,對(duì)remain字符串進(jìn)行分割處理。
tokens=1*,tokens表示分段的方式,tokens=1*表示第一個(gè)分隔符;之前的作為一部分,剩下的(*表示)作為一部分。這兩部分在循環(huán)體總可以用%%a表示第一部分,%%b表示第二部分。
批處理 遍歷path環(huán)境變量
我們知道path環(huán)境變量也是以分號(hào)作為分隔符的,批處理中,所以同樣可以用上面的代碼來遍歷path環(huán)境變量。
@echo off setlocal enabledelayedexpansion ::定義一個(gè)以分號(hào)作為分隔的字符串 set str=%path% ::str的副本 set remain=%str% :loop for /f "tokens=1* delims=;" %%a in ("%remain%") do ( ::輸出第一個(gè)分段(令牌) echo %%a rem 將截取剩下的部分賦給變量remain,其實(shí)這里可以使用延遲變量開關(guān) set remain=%%b ) ::如果還有剩余,則繼續(xù)分割 if defined remain goto :loop pause
運(yùn)行結(jié)果:
D:\dev\workspace\MarkdownTools
......
C:\windows\system32
D:\dev\java\jdk1.8.0_91\bin
F:\Program Files\nodejs\node_global
F:\Program Files\Git\bin
D:\dev\apache-maven-3.5.4\bin
......
請(qǐng)按任意鍵繼續(xù). . .
批處理 判斷path環(huán)境變量中是否有某個(gè)目錄
例如查找系統(tǒng)path環(huán)境變量中是否存在D:\dev\workspace\MarkdownTools這個(gè)目錄:
@echo off setlocal enabledelayedexpansion ::定義一個(gè)以分號(hào)作為分隔的字符串 ::set str=AAA;BBB;CCC;DDD;EEE;FFF set str=%path% ::str的副本 set remain=%str% set toFind=D:\dev\workspace\MarkdownTools set isFind=false :loop for /f "tokens=1* delims=;" %%a in ("%remain%") do ( if "%toFind%"=="%%a" ( ::設(shè)置標(biāo)記,以便后續(xù)使用 set isFind=true ::找到了就不找了 goto :finded ) rem 將截取剩下的部分賦給變量remain,其實(shí)這里可以使用延遲變量開關(guān) set remain=%%b ) ::如果還有剩余,則繼續(xù)分割 if defined remain goto :loop :finded echo %isFind% pause
運(yùn)行結(jié)果:
true
請(qǐng)按任意鍵繼續(xù). . .
參考資料
最近有有個(gè)小需求需要將shell 腳本的功能挪到windows中,但發(fā)現(xiàn)shell中有數(shù)組概念,但windows中卻沒有,同時(shí)shell中有很多方式處理字符串分割,但bat中就顯得比較雞肋,經(jīng)過一番查找,終于有了方案(Stack Overflow:http://stackoverflow.com/questions/1707058/how-to-split-a-string-in-a-windows-batch-file):
方案: 通過for循環(huán)處理,而處理的方式又可以分兩種,一種是普通for,一種是for的文件處理方式:
方案一:
@echo off & setlocal rem 注意這里的s定義,其值不是使用雙引號(hào)引起來的 rem also works for comma-separated lists, e.g. ABC,DEF,GHI,JKL set s=AAA BBB CCC DDD EEE FFF for %%a in (%s%) do echo %%a
方案二:is the best for (most) arbitrary delimiter characters.
@echo off & setlocal set s=AAA BBB CCC DDD EEE FFF set t=%s% :loop for /f "tokens=1*" %%a in ("%t%") do ( echo %%a rem 將截取剩下的部分賦給t,其實(shí)這里可以使用延遲變量開關(guān) set t=%%b ) if defined t goto :loop
有個(gè)老兄給了個(gè)更完整的(用到了延遲變量):
@echo off setlocal ENABLEDELAYEDEXPANSION REM Set a string with an arbitrary number of substrings separated by semi colons set teststring=The;rain;in;spain REM Do something with each substring :stringLOOP REM Stop when the string is empty if "!teststring!" EQU "" goto END for /f "delims=;" %%a in ("!teststring!") do set substring=%%a REM Do something with the substring - REM we just echo it for the purposes of demo echo !substring! REM Now strip off the leading substring :striploop set stripchar=!teststring:~0,1! set teststring=!teststring:~1! if "!teststring!" EQU "" goto stringloop if "!stripchar!" NEQ ";" goto striploop goto stringloop ) :END endlocal
還有這樣的:
set input=AAA BBB CCC DDD EEE FFF set nth=4 for /F "tokens=%nth% delims= " %%a in ("%input%") do set nthstring=%%a echo %nthstring%
其實(shí)Powershell里可能有更多的內(nèi)置函數(shù)可以使用:
PS C:\> "AAA BBB CCC DDD EEE FFF".Split()
還有人提出用vbscrip代替bat:
Set objFS = CreateObject("Scripting.FileSystemObject") Set objArgs = WScript.Arguments str1 = objArgs(0) s=Split(str1," ") For i=LBound(s) To UBound(s) WScript.Echo s(i) WScript.Echo s(9) ' get the 10th element Next usage: c:\test> cscript /nologo test.vbs "AAA BBB CCC"
最后來一個(gè)bat中的小難點(diǎn): 變量延遲(自上而下,逐條(簡(jiǎn)單語句、復(fù)合語句(for、if 語句塊只算一條))執(zhí)行,而非逐行執(zhí)行)
變量延遲詳解 call setlocal
以上就是批處理中字符串分割實(shí)現(xiàn)代碼的詳細(xì)內(nèi)容,更多關(guān)于批處理字符串分割的資料請(qǐng)關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。