批处理去除空格

去掉左侧空格1:
  1. @echo off
  2. set "str=   ab c&>!   "
  3. for /f "tokens=*" %%i in ("%str%") do echo "☆%%i☆"
  4. pause
复制代码
  去掉左侧空格2:
  1. @echo off
  2. set "str=   ab c&>!   "
  3. :intercept
  4. if "%str:~0,1%"==" " set "str=%str:~1%"&goto intercept
  5. echo "☆%str%☆"
  6. pause
复制代码
  去掉右侧空格1:
  1. @echo off
  2. set "str=   ab c&>!   "
  3. for /f "delims=" %%i in ("%str%") do echo "☆%%~nxi☆"
  4. pause
复制代码
  去掉右侧空格2:
  1. @echo off
  2. set "str=   ab c&>!   "
  3. :intercept
  4. if "%str:~-1%"==" " set "str=%str:~0,-1%"&goto intercept
  5. echo "☆%str%☆"
  6. pause
复制代码
  去掉首尾空格1:
  1. @echo off
  2. set "str=   ab c&>!   "
  3. for /f "tokens=*" %%i in ("%str%") do echo "☆%%~nxi☆"
  4. pause
复制代码
  去掉首尾空格2:
  1. @echo off
  2. set "str=   ab c&>!   "

  3. :intercept_left
  4. if "%str:~0,1%"==" " set "str=%str:~1%"&goto intercept_left

  5. :intercept_right
  6. if "%str:~-1%"==" " set "str=%str:~0,-1%"&goto intercept_right
  7. echo "☆%str%☆"
  8. pause
  去掉所有空格:
  1. @echo off
  2. set "str=   ab c&>!   "
  3. set "str=%str: =%"
  4. echo "☆%str%☆"
  5. pause
复制代码
  以上代码能兼容除双引号外的其他特殊字符。
posted @ 2011-12-07 16:06  zhh  阅读(1704)  评论(0编辑  收藏  举报