问题描述
假设我们有一个变量;
set test =这是一个测试字符串。
,我们要替换每个小写 t
使用大写 X
所需的输出;
TesX sXring。
我尝试使用字符串操作 set test =%test:t = X%
,但它用 X
替换每个 t
。我也试过从@dbenham的,但不能
没有本地批处理命令可以方便地进行区分大小写的字符串操作。原生批处理解决方案必须在循环中逐个字符地构建一个新的字符串。
EDIT - 很久以前,我写了一个纯粹的批处理实用程序,名为执行区分大小写查找/替换的内容文本文件。性能不错,但它有一些限制。我很少使用该代码。我写了一个配对例程来做一个变量而不是文本文件的大小写查找/替换,但我似乎已经失去了该代码。
今天我几乎总是使用每当我想要操作文本。
解决方案应该是:
for / f delims ^ = ^ eol ^ = %% A in('jrepl t X / s test')do settest = %% A
b $ b
但似乎有一个错误。如果变量名称是除 test
之外的某个名称,它适用。看起来我有一些调试要做。
在修复错误之前,您可以使用:
for / f delims ^ = ^ eol ^ = %% A in('cmd / v:on / cecho(!test!)| jrepl t X %% A
更新:在版本3.4中 / S
选项现在可以使用名为 TEST
/ p>
Lets say we have a variable like;
set test=This is a Test string.
and we want to replace every lower-case t
with upper-case X
so desired output;
This is a TesX sXring.
i tryed using string manipulation set test=%test:t=X%
but its replacing every t
with X
. I also tryed JREPL.bat from @dbenham but cant work it out.
There is no native batch command that can conveniently do case sensitive string manipulation. A native batch solution would have to build a new string character by character in a loop. Very doable - but a pain and also inefficient.
EDIT - Long ago I wrote a pure batch utility called modFile.bat that does a case sensitive find/replace on the content of a text file. Performance isn't bad, but it does have some restrictions. I rarely use that code. I had written a companion routine to do case sensitive find/replace on a variable instead of a text file, but I seem to have lost that code.
Today I pretty much always use JREPL.BAT whenever I want to manipulate text.
The JREPL.BAT solution for your example should be:
for /f delims^=^ eol^= %%A in ('jrepl t X /s test') do set "test=%%A"
But there seems to be a bug. It works if the variable name is some name other than test
. Looks like I have some debugging to do.
Until the bug is fixed, you can use:
for /f delims^=^ eol^= %%A in ('cmd /v:on /c "echo(!test!)|jrepl t X"') do set "test=%%A"
Update: The bug has been fixed in version 3.4 The /S
option now works with a variable named TEST
.
这篇关于字符串操作与区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!