Is it possible to define a delimiter which is not limited to 1 character? Based on the title's example, I would like to define my separator as e.g. '#+#'. Textfiles/lines can contain both characters, but there is very little chance you'll come across that particular substring/text combo.
相关问题
- Inheritance impossible in Windows Runtime Componen
- how to get running process information in java?
- Is TWebBrowser dependant on IE version?
- How can I have a python script safely exit itself?
- I want to trace logs using a Macro multi parameter
相关文章
- 在vscode如何用code runner打开独立的控制台窗口,以及设置好调试模式时窗口的编码?
- 如何让cmd.exe 执行 UNICODE 文本格式的批处理?
- 怎么把Windows开机按钮通过修改注册表指向我自己的程序
- Warning : HTML 1300 Navigation occured?
- Bundling the Windows Mono runtime with an applicat
- Windows 8.1 How to fix this obsolete code?
- CosmosDB emulator can't start since port is al
- How to print to stdout from Python script with .py
I recently discovered an interesting trick that allows to use a multi-character string as delimiter to split a larger string in a very simple way; the method does not use any
for
command, but perform the split in just one line! Here it is:This method also allows to split a large string in several parts and store all of them in an array. Further details at dos batch iterate through a delimited string
No, you can not use a string as a delimiter in the
delims=
clause. Of course you can include the string, but it will be handled as a set of separate characters that will be used as delimiters, not as a delimiter string.If you really need to split on a string, the fastest approach could be to replace the delimiter string by a character not included in the data and use this character as delimiter
Note: The
setlocal enabledelayedexpansion
is needed to be able to read the variable changed inside thefor
loop retrieving the data (here simulated directly including a string). Then, inside thefor
loop that tokenizes the readed line, delayed expansion is disabled to avoid problems with the!
characters (if delayed expansion is active, they will be consumed by the parser). This is the reason for theendlocal
inside the loop.As we are doing a string replacement and it is possible to end with a string composed of only delimiters, it is possible that the
do
clause of the innerfor
will not be executed, so the finalif
is included to ensure that theenabledelayedexpansion
is cancelled.