每x个字符批量添加一个字符 [英] Batch adding a character every x characters

查看:110
本文介绍了每x个字符批量添加一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我用%1获取参数,并且它是服务器",如何在每个字母后添加一个+号? 所以我的结果将是"S + e + r + v + e + r"?

If I get my parameter with %1 and it is "Server" how can I add a + sign after every letter? So my result would be "S+e+r+v+e+r"?

我认为

I think Batch file to add characters to beginning and end of each line in txt file this is a similar question but I don't know how to change the code for this purpose.

任何帮助都会很棒!

推荐答案

我很确定之前已经有人问过这个问题,但我找不到它.

I'm pretty sure this has been asked and answered before, but I couldn't find it.

我在某处看到了一个非常酷(快速)的解决方案.它使用带有/U选项的新cmd.exe进程,因此输出采用unicode.关于Unicode的有趣之处在于,每个ASCII字符都表示为自己,后跟一个nul字节(0x00).将其通过管道传递给MORE时,会将nul字节转换为换行符!然后,使用FOR/F迭代每个字符并构建所需的字符串.最后的子字符串操作用于从前面删除多余的+.

There is a really cool (and fast) solution that I saw posted somewhere. It uses a new cmd.exe process with the /U option so output is in unicode. The interesting thing about the unicode is that each ASCII character is represented as itself followed by a nul byte (0x00). When this is piped to MORE, it converts the nul bytes into newlines!. Then a FOR /F is used to iterate each of the characters and build the desired string. A final substring operation is used to remove the extra + from the front.

我稍微调整了代码的存储空间,使用转义序列玩游戏,以使延迟的扩展在正确的时间发生,并在附加字符时保护该字符-所有这些都是为了保留该技术^!字符.使用此一般技术,这可能是对现有已发布代码的新扭曲.

I tweaked my memory of the code a bit, playing games with escape sequences in order to get the delayed expansion to occur at the correct time, and to protect the character when it is appended - all to get the technique to preserve ^ and ! characters. This may be a new twist to existing posted codes using this general technique.

@echo off
setlocal enableDelayedExpansion
set "str=Server bang^! caret^^"
set "out="
for /f delims^=^ eol^= %%A in ('cmd /u /v:on /c echo(^^!str^^!^|more') do set "out=!out!+^%%A"
set "out=!out:~1!"
echo Before: !str!
echo  After: !out!

-输出---

Before: Server bang! caret^
 After: S+e+r+v+e+r+ +b+a+n+g+!+ +c+a+r+e+t+^

这篇关于每x个字符批量添加一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆