如何批量移动句子中的空格

时间:2013-04-09 22:18:31

标签: batch-file

这是我的问题。我做了一个批处理,想要从句子中移动单词之间的所有空格。 我这样做了。但我想将空间设置为变量,并在set命令中使用该变量。 在对变量进行cheking定义之后,甚至很好地回应了这个空间变量,我无法移动 它在最后一句话。有人可以告诉我为什么?

@echo off

::why this doesnt work???


(set /p word2=i will make )<nul>textfile.txt

<nul (set/p word3=all strings be )>>textfile.txt

<nul (set/p word4=one longstring)>>textfile.txt

echo(

type textfile.txt

echo(

echo(

::seting space

set sp=a b

set sa=%sp:a=%

set space=%sa:b=%

if defined space (echo space is defined) else (echo space is not defined)

echo(

echo lets see:%space%%space%%space%something%space%something%space%%space%, ...works

echo(

set /p inputalltext=<textfile.txt

echo %inputalltext%...input from textfile

set noplaceforspace=%inputalltext: =% ...as you see as normal,no space

echo %noplaceforspace%

::so, problem starts here. why are here  places if space is defined and good echoing

::of space variable??

setlocal enabledelayedexpansion

set nogoodresult=%inputalltext:!space!=%

echo %nogoodresult%%space%%space% here i dont want space between words

echo(

pause

2 个答案:

答案 0 :(得分:1)

请看我的例子:

@echo off&setlocal
set "space= "
set "sentence=this is a long sentence with many words "

setlocal enabledelayedexpansion
set "sentence=!sentence:%space%=!"
echo "%sentence%"

答案 1 :(得分:0)

如果你正在运行一个普通的批处理,你应该可以做“句子句子”,只要你有“”句子。系统应该输入一个空格