Board logo

标题: [已结]字符串或文本中的感叹号如何替换? [打印本页]

作者: lqh123108     时间: 2008-12-25 21:50    标题: [已结]字符串或文本中的感叹号如何替换?
现有字符串
 "真好呀!!!"
如何替换成 "真好呀@@@" 这是我的测试代码:
setlocal  ENABLEDELAYEDEXPANSION
for /f %%a in (1.txt) do (
set str=%%a
set str=!str:^!=@!
echo !str!
pause)
[ Last edited by HAT on 2008-12-26 at 10:45 ]

作者: HAT     时间: 2008-12-25 21:53
set /? Environment variable substitution has been enhanced as follows: %PATH:str1=str2% would expand the PATH environment variable, substituting each occurrence of "str1" in the expanded result with "str2". "str2" can be the empty string to effectively delete all occurrences of "str1" from the expanded output. "str1" can begin with an asterisk, in which case it will match everything from the beginning of the expanded output to the first occurrence of the remaining portion of str1.

作者: lqh123108     时间: 2008-12-25 22:02    标题: 帮我再看看,文本中怎么不行?
多谢了 为什么我直接在CMD窗口中行的 如果该字符串是在文本中,我用FOR去读取,再替换,怎么不成??

作者: HAT     时间: 2008-12-25 22:03    标题: Re 3楼
把你的完整代码、详细问题描述更新到顶楼

作者: lqh123108     时间: 2008-12-25 22:05
Originally posted by HAT at 2008-12-25 22:03: 把你的完整代码、详细问题描述更新到顶楼
文本的内容就只有上面这个字符串呀!

作者: HAT     时间: 2008-12-25 22:11
作者: lqh123108     时间: 2008-12-25 22:42    标题: 还是得不到结果
认真看了一下,学了不少! 有些地方还是不太懂!! 但测试来测试去,还是不得解,这代码到底该怎么写呀? for /f %%a in (1.txt) do ( set "str=%%a" setlocal ENABLEDELAYEDEXPANSION set str=!str:^!=@! echo !str! pause) [ Last edited by lqh123108 on 2008-12-25 at 22:44 ]

作者: moniuming     时间: 2008-12-25 23:28
@echo off
for /f "delims=" %%a in ('more +7^<%~fs0') do (
  set "str=%%a"
  call set "str=%%str:!=@%%"
  call echo.%%str%%
)
pause>nul&goto :eof
"真好呀!!!"
[ Last edited by moniuming on 2008-12-25 at 23:33 ]

作者: lqh123108     时间: 2008-12-26 10:43
多谢猫兄的指点 谢了 针对文件通过! OK! for /f "delims=" %%a in (1.txt) do ( set "str=%%a" call set "str=%%str:!=@%%" call echo.%%str%% ) pause>nul&goto :eof 看来,文本中的感叹号的替换不能启用变量延迟,而应该使用跳出循环的办法,防止歧义!