在批处理中用新行替换字符串 [英] Replace string with a new line in Batch
本文介绍了在批处理中用新行替换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
set NLM=^
set NL=^^^%NLM%%NLM%^%NLM%%NLM%
SET memoli=%token:QMZ=%NL%%%
echo %memoli%>>%tmp%\list2.txt
我不能用新行更改字符串"QMZ".该怎么做?
I cant change the string "QMZ" with a new line. How to do that?
推荐答案
非常简单
setlocal EnableDelayedExpansion
set "token=HelloQMZworld"
echo !token:QMZ=^
!
它用作批处理解析器首先解析多行插入符,然后将其替换为单个换行符.
然后,在延迟扩展阶段,它将QMZ替换为单个换行符,这在该阶段是合法的.
It works as the batch parser parses first the multiline caret and replace it with a single linefeed.
Then in the delayed expansion phase it replaces the QMZ with a single linefeed, which is legal in that phase.
要使用替换后的字符串设置新变量,只需使用
To set a new variable with the replaced string simply use
setlocal EnableDelayedExpansion
set "token=HelloQMZworld"
set newVal=!token:QMZ=^
!
echo !newVal!
这篇关于在批处理中用新行替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文