修改了我原来写的一个字符切分过程。不得已,必须破坏模块的局部特性,否则不能处理英文的!。
字符切分的代码可以切分除换行符之外的几乎所有可显字符。没试过其他标点符号,但分号,逗号,引号,括号,空格肯定可以
虽然常见到VBS,JS,和其他语言中有字符切分的函数,但批处理中没有,就写了一个。处理的文本放在1.txt中。效率肯定没
其他语言写的高。- @echo off
- set __yiwuyun.txt=___yiwuyun.txt
- set quotation=^"
- echo 1 1>___m1.txt
- for /f "delims=" %%a in ('findstr /n .* 1.txt') do (
- set "string=%%a"
- setlocal EnableDelayedExpansion
- set string=!string:*:=!
- set "temp="
- call :GetTokenTextNAndCount "^!string^!" "^!quotation^!"
- set /p m1=<___m1.txt
- for /l %%b in (1,1,!tokenTextCount!) do (
- if %%b equ !tokenTextCount! (
- set "temp=!temp!!tokenText%%b!"
- ) else (
- set /a m=!m1!%%2
- if !m! equ 0 (
- set "temp=!temp!!tokenText%%b!”"
- set /a m1+=1
- echo !m1!>___m1.txt
- ) else (
- set "temp=!temp!!tokenText%%b!“"
- set /a m1+=1
- echo !m1!>___m1.txt
- )
- )
- )
- echo.!temp!
- endlocal
- )
- if exist ___m1.txt del ___m1.txt
- pause
- exit /b 0
-
-
- :GetStringLength
- setlocal
- set string=!string!
- set /a n=0
- :startGetStringLength
- if not "!string!"=="" (
- set string=!string:~1!
- set /a n=!n!+1
- goto startGetStringLength
- )
- set /a stringLength=!n!
- endlocal&set stringLength=%stringLength%
- exit /b 0
-
-
- :GetTokenPlaceNAndCount
- setlocal
- set "string=%~1"
- set "secondParameter=%~2"
- call :GetStringLength
- set /a nGetTokenPlaceNAndCount=!stringLength!-1
- set /a nToken=1
- set /a tokenPlace0=-1
- for /l %%a in (0,1,!nGetTokenPlaceNAndCount!) do (
- if "!string:~%%a,1!"=="!secondParameter!" (
- set /a tokenPlace!nToken!=%%a
- set /a nToken+=1
- )
- )
- set /a tokenPlace!nToken!=!stringLength!
- set /a tokenCount=!nToken!-1
- type nul>!__yiwuyun.txt!
- for /l %%a in (0,1,!nToken!) do (
- echo set /a tokenPlace%%a=!tokenPlace%%a!>>!__yiwuyun.txt!
- )
- echo set /a tokenCount=!tokenCount!>>!__yiwuyun.txt!
- endlocal
- for /f "tokens=1 delims=" %%a in ('type !__yiwuyun.txt!') do (
- rem echo %%a
- %%a
- )
- if exist !__yiwuyun.txt! del !__yiwuyun.txt!
- exit /b 0
-
-
- :GetTokenTextNAndCount
- rem 该函数必须去掉局部性,要不然不能处理感叹号。
- rem setlocal
- set "string=%~1"
- set "___secondParameter=%~2"
- rem if "!___secondParameter!"==" " (
- rem call :GetStringTrimRedundanceSpace
- rem set string=!stringTrimRedundanceSpace!
- rem )
- call :GetTokenPlaceNAndCount "^!string^!" "^!___secondParameter^!"
- set /a ___next=0
- set /a tokenTextCount=!tokenCount!+1
- set /a ___n=0
- :startGetTokenTextNAndCount
- set /a ___beforePlace=!tokenPlace%___next%!
- set /a ___first=!___beforePlace!+1
- set /a ___next=!___next!+1
- set /a ___afterPlace=!tokenPlace%___next%!
- set /a ___second=!___afterPlace!-!___beforePlace!-1
- set /a ___n=!___n!+1
- if "!string!"=="" (
- set "tokenText!___n!="
- ) else (
- set tokenText!___n!=!string:~%___first%,%___second%!
- )
- if !___n! lss !tokenTextCount! goto startGetTokenTextNAndCount
- rem type nul>!__yiwuyun.txt!
- rem for /l %%a in (1,1,!tokenTextCount!) do (
- rem echo set "tokenText%%a=!tokenText%%a!">>!__yiwuyun.txt!
- rem )
- rem echo set /a tokenTextCount=!tokenTextCount!>>!__yiwuyun.txt!
- rem endlocal
- rem for /f "tokens=1 delims=" %%a in ('type %__yiwuyun.txt%') do (
- rem echo %%a
- rem %%a
- rem )
- rem if exist %__yiwuyun.txt% del %__yiwuyun.txt%
- exit /b 0
复制代码
|