网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Bat处理字符串分割(split功能)-创新互联

最近有有个小需求需要将shell 脚本的功能挪到windows中,但发现shell中有数组概念,但windows中却没有,同时shell中有很多方式处理字符串分割,但bat中就显得比较鸡肋,经过一番查找,终于有了方案(Stack Overflow:http://stackoverflow.com/questions/1707058/how-to-split-a-string-in-a-windows-batch-file):

专业领域包括网站制作、网站设计、商城网站建设、微信营销、系统平台开发, 与其他网站设计及系统开发公司不同,成都创新互联公司的整合解决方案结合了帮做网络品牌建设经验和互联网整合营销的理念,并将策略和执行紧密结合,为客户提供全网互联网整合方案。

方案: 通过for循环处理,而处理的方式又可以分两种,一种是普通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,其实这里可以使用延迟变量开关
   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里可能有更多的内置函数可以使用:

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中的小难点: 变量延迟(自上而下,逐条(简单语句、复合语句(for、if 语句块只算一条))执行,而非逐行执行)

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

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

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

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享题目:Bat处理字符串分割(split功能)-创新互联
标题网址:http://bjjierui.cn/article/dpsioe.html

其他资讯