快捷搜索:  汽车  科技

批处理判断字符串为空(n种方法-去除字符串中的空格)

批处理判断字符串为空(n种方法-去除字符串中的空格)@echo off setlocal enableDelayedExpansion set "str= I love you . " :stripRight if "!str:~-1!"==" " ( set "str=%str:~0 -1%" goto stripRight ) else @echo "%str%" endlocal pause去除右边空格 之二:利用变量扩展@echo off setlocal enableDelayedExpansion set "str= I love you . 66 " @echo 原字符串无引号:"%str%" call :stripByCall "%str%" goto end :stripByCal

用*替换空格:用字符替换命令

set str=" I love you ! "

用“*”号替换空格,命令: %str: =*%

批处理判断字符串为空(n种方法-去除字符串中的空格)(1)

去除所有空格

命令:%str: =%

批处理判断字符串为空(n种方法-去除字符串中的空格)(2)

去除左空格 之一:用到截取字符命令%str:~n m%

批处理判断字符串为空(n种方法-去除字符串中的空格)(3)

@echo off setlocal enableDelayedExpansion set "str= I love you . " :stripLeft if "!str:~ 1!"==" " ( set "str=%str:~1%" goto stripLeft ) else @echo "%str%" endlocal pause

去除左空格 之二:利用for循环中变量自动去掉左空格

批处理判断字符串为空(n种方法-去除字符串中的空格)(4)

@echo off setlocal enableDelayedExpansion set "str= I love you . 66 " @echo 原字符串无引号:"%str%" call :stripByCall "%str%" goto end :stripByCall for /f "tokens=*" %%i in (%1) do SET str=%%i @echo 去掉 左空格后:"%str%" goto :eof :end endlocal pause

去除右边空格 之一:用到截取字符命令%str:~-n m%

批处理判断字符串为空(n种方法-去除字符串中的空格)(5)

@echo off setlocal enableDelayedExpansion set "str= I love you . " :stripRight if "!str:~-1!"==" " ( set "str=%str:~0 -1%" goto stripRight ) else @echo "%str%" endlocal pause

去除右边空格 之二:利用变量扩展

批处理判断字符串为空(n种方法-去除字符串中的空格)(6)

@echo off setlocal enableDelayedExpansion set "str= I love you . 66 " @echo 原字符串无引号:"%str%" call :stripByCall "%str%" goto end :stripByCall set str=%~nx1 @echo 去掉右空格后:"%str%" goto :eof :end endlocal pause

去除左右空格:利用扩展变量%~nxi

批处理判断字符串为空(n种方法-去除字符串中的空格)(7)

@echo off setlocal enableDelayedExpansion set "str= I love you . 66 " @echo 原字符串无引号:"%str%" call :stripByCall "%str%" goto end :stripByCall for /f "tokens=*" %%i in (%1) do SET str=%%~nxi @echo 去掉左右空格后:"%str%" goto :eof :end endlocal pause

判断开头字符串:findstr "^ab"判断以“ab”开头,errorlevel=0表示成功

批处理判断字符串为空(n种方法-去除字符串中的空格)(8)

判断结尾字符串:findstr "ab$"判断以“ab”结尾,errorlevel<>0表示失败

批处理判断字符串为空(n种方法-去除字符串中的空格)(9)

匹配字符集中任意一个:字符集放入[]中,命令findstr [],字符集可写成:[0-9] [a-z] [c-ex-z] "a[bcd]u[123]"即"abu1” ... "adu3"中的一个

批处理判断字符串为空(n种方法-去除字符串中的空格)(10)

批处理判断字符串为空(n种方法-去除字符串中的空格)(11)

3不在0-2和5-9之间

批处理判断字符串为空(n种方法-去除字符串中的空格)(12)

6在5-9之间

任意一个不匹配字符集:findstr [^] 只要任意一个不匹配字符集[]成功

批处理判断字符串为空(n种方法-去除字符串中的空格)(13)

批处理判断字符串为空(n种方法-去除字符串中的空格)(14)

批处理判断字符串为空(n种方法-去除字符串中的空格)(15)

猜您喜欢: