Windows批处理BAT字符串操作 && Windows批处理函数编程

来源:互联网 发布:法语考研方向知乎 编辑:程序博客网 时间:2024/06/02 23:03

Windows批处理BAT字符串操作,并且把字符串操作提取成了函数调用


首先是字符串应用举例:(1)截取(2)获得长度(3)查找字符首次出现位置(4)查找字符最后出现位置。有的是直接写的逻辑,有的是函数调用。

然后是各个函数的描述:(1)strlen()(2)strchr()(3)last_index()。

不过注释是英文的,有些不够详尽,但是代码应该还能看^_^。

代码如下:
注意有setlocal EnableDelayedExpansion

@echo off&setlocal EnableDelayedExpansion rem rem (1)rem src_str.substring(start_pos, length)rem set result=%src_str:~start_pos,length% rem set str1=This is string1set str2=%str1:~8,6%set str3=%str1:~0,4%set str4=%str1:~5%echo src_str:echo str1=%str1%echo result strings:echo str2=%str2%echo str3=%str3%echo str4=%str4%echo/rem rem (2)rem strlen()rem set str1=This is a test stringset str2=Hello Worldset num=0set str_tmp=%str1%:next1if not "%str_tmp%"=="" (set /a num+=1set "str_tmp=%str_tmp:~1%"goto next1)echo str1=%str1%echo str1's length is %num%set num=0set str_tmp=%str2%:next2if not "%str_tmp%"=="" (set /a num+=1set "str_tmp=%str_tmp:~1%"goto next2)echo str2=%str2%echo str2's length is %num%echo/rem rem (3)rem strchr()rem return 1~length, 0 indicate the char doesn't existrem rem setlocal EnableDelayedExpansionset str1=This is a test stringset ch1=tset num=0set str_tmp=%str1%:nextif not "%str_tmp%"=="" (set /a num+=1rem check the fisrt char of this stringif "!str_tmp:~0,1!"=="%ch1%" goto lastrem get remaining stringset "str_tmp=%str_tmp:~1%"goto next)set /a num=0:lastecho the first occurrence of char '%ch1%' in "%str1%" is %num%set pre_str=!str1:~0,%num%!echo then we get prefix str: "%pre_str%".set suf_str=!str1:~%num%!echo then we get suffix str: "%suf_str%".call :strchr str1 %ch1% indexecho strchr() '%ch1%' in "%str1%" is %index%set ch1=acall :strchr str1 %ch1% index 2echo strchr() '%ch1%' in "%str1%" is %index%echo/rem rem (4)rem lastIndex()rem return 1~length, 0 indicate the char doesn't existrem set str1=This is a test stringset ch1=tcall :last_index str1 %ch1% last_indexecho the last occurrence of char '%ch1%' in "%str1%" is %last_index%.set pre_str=!str1:~0,%last_index%!echo then we get prefix str: "%pre_str%".set suf_str=!str1:~%last_index%!echo then we get suffix str: "%suf_str%".set ch1=scall :last_index str1 %ch1% last_index22echo the last occurrence of char '%ch1%' in "%str1%" is %last_index22%.goto finishrem =======================================================================================rem rem the follow code: i have extracted strlen(), strchr(), lastIndex() into method(or funtion)rem rem =======================================================================================rem ----Begin strlen()---------------------------------------------------------rem rem strlen()rem goto finish:strlen src_str lenrem here we transfer src_str via reference, becall this way allow space charssetlocalset num=0rem the follow call must start with callcall set str_tmp=%%%1%%:_strlen_next1if not "%str_tmp%"=="" (set /a num+=1set "str_tmp=%str_tmp:~1%"goto _strlen_next1)endlocal & set "%2=%num%"goto :EOFrem ----End strlen()-----------------------------------------------------------rem ----Begin strchr()---------------------------------------------------------rem rem strchr() or first_index() methodrem return 1~length, 0 indicate the char doesn't existrem goto finish:strchr src_str %char% result %from%rem here we transfer src_str via reference, becall this way allow space charssetlocal EnableDelayedExpansionset num=0rem the follow call must start with callcall set str_tmp=%%%1%%set target_ch=%2set from_pos=0if not "%4"=="" set /a from_pos=%4if %from_pos% GTR 1 set /a from_pos-=1if %from_pos% GEQ 1 set str_tmp=!str_tmp:~%from_pos%!set /a num+=%from_pos%:_first_index_next1if not "%str_tmp%"=="" (set /a num+=1rem check the fisrt char of this stringif "!str_tmp:~0,1!"=="%target_ch%" goto _first_index_lastrem get remaining stringset "str_tmp=%str_tmp:~1%"goto _first_index_next1)set /a num=0:_first_index_lastendlocal & set "%3=%num%"goto :EOFrem ----End strchr()-----------------------------------------------------------rem ----Begin last_index-------------------------------------------------------rem rem last_index()rem return 1~length, 0 indicate the char doesn't existrem goto finish:last_index src_str %char% resultrem here we transfer src_str via reference, becall this way allow space charssetlocal EnableDelayedExpansionrem the follow call must start with callcall set str_tmp=%%%1%%set len=0rem fisrt get the length of this stringcall :strlen str_tmp lenrem echo the length is %len%set target_ch=%2:_last_index_next1if not "%str_tmp%"=="" (rem check the last char of this stringif "!str_tmp:~-1!"=="%target_ch%" goto _last_index_lastrem get remaining stringset /a len-=1set "str_tmp=%str_tmp:~0,-1%"goto _last_index_next1)set /a len=0:_last_index_lastendlocal & set "%3=%len%"goto :EOFrem ----End last_index---------------------------------------------------------:finish


0 0
原创粉丝点击