Bat處理字符串分割(split功能)-創(chuàng)新互聯(lián)

最近有有個小需求需要將shell 腳本的功能挪到windows中,但發(fā)現(xiàn)shell中有數(shù)組概念,但windows中卻沒有,同時shell中有很多方式處理字符串分割,但bat中就顯得比較雞肋,經(jīng)過一番查找,終于有了方案(Stack Overflow:http://stackoverflow.com/questions/1707058/how-to-split-a-string-in-a-windows-batch-file):

專業(yè)領(lǐng)域包括網(wǎng)站制作、網(wǎng)站設(shè)計、商城網(wǎng)站建設(shè)、微信營銷、系統(tǒng)平臺開發(fā), 與其他網(wǎng)站設(shè)計及系統(tǒng)開發(fā)公司不同,成都創(chuàng)新互聯(lián)公司的整合解決方案結(jié)合了幫做網(wǎng)絡(luò)品牌建設(shè)經(jīng)驗和互聯(lián)網(wǎng)整合營銷的理念,并將策略和執(zhí)行緊密結(jié)合,為客戶提供全網(wǎng)互聯(lián)網(wǎng)整合方案。

方案: 通過for循環(huán)處理,而處理的方式又可以分兩種,一種是普通for,一種是for的文件處理方式:

方案一:

@echo off & setlocal
rem 注意這里的s定義,其值不是使用雙引號引起來的
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,其實這里可以使用延遲變量開關(guān)
   set t=%%b
   )
if defined t goto :loop

有個老兄給了個更完整的(用到了延遲變量):

@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%

其實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"

最后來一個bat中的小難點(diǎn): 變量延遲(自上而下,逐條(簡單語句、復(fù)合語句(for、if 語句塊只算一條))執(zhí)行,而非逐行執(zhí)行)

http://blog.csdn.net/icebergsnowfox/article/details/8047613

http://cndos.fam.cx/forum/

http://ss64.com/  - 哎吆,不錯哦

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)頁名稱:Bat處理字符串分割(split功能)-創(chuàng)新互聯(lián)
標(biāo)題路徑:http://muchs.cn/article4/dpsioe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計、商城網(wǎng)站、App開發(fā)、微信小程序、定制開發(fā)營銷型網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)