批量替换字符串不包含FOR循环

批量替换字符串不包含FOR循环

本文介绍了批量替换字符串不包含FOR循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下字符串

set str=aaaaa,bbbbb,ccccc,dddd

我需要更换一切从第一(逗号),直到字符串的结尾。但我不知道怎么的字符串开始。

I need to replace everything from the first , (comma) till the end of string. but I don't know how the string starts.

我不能使用通配符*,因为它是不是在字符串的开始,所以类似的东西 -

I can't use the * wildcard as it is not in the start of string, so something like that -

set str=%str:,*=% >>>> Will not work

任何想法?它必须在一个批处理脚本

Any ideas ? it must be in a batch script

推荐答案

有就是避免了FOR循环一个非常简单而快速的解决方案:使用搜索和替换在每个逗号注入级联REM命令: - )

There is a very simple and fast solution that avoids a FOR loop: use search and replace to inject a concatenated REM command at each comma :-)

@echo off
set str=aaaaa,bbbbb,ccccc,dddd
set "str=%str:,="&rem %
echo str=%str%

说明

下面是code的重要防线,任何扩大发生之前:

Here is the the important line of code, before any expansion takes place:

set "str=%str:,="&rem %

这是该行被转换成变量扩展后:

And here is what the line is transformed into after variable expansion:

set "str=aaaaa"&rem bbbbb"&rem ccccc"&rem dddd

它成为一个SET语句,然后是REM语句。在&安培; 运营商可将多个命令在一行结合起来。世界上只有一个REM语句,因为第一个REM之后一切都被认为此言的一部分。

It becomes a SET statement, followed by a REM statement. The & operator allows you to combine multiple commands on one line. There is only one REM statement because everything after the first REM is considered part of the remark.

这篇关于批量替换字符串不包含FOR循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:59