K
K
KTG2019-03-17 17:05:25
cmd/bat
KTG, 2019-03-17 17:05:25

How to count the number of delimiters in a string?

There is a line
значение1^значение2^значение3^
How to count the number of delimiters?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
res2001, 2019-03-17
@res2001

The cap symbol ^is "special" to cmd. So it should not be used as a separator if you plan to solve the problem on batch files.
In general, your problem can be solved like this:

@echo off

set "str=hjh#jhjh#hkjh#khkjh##khkjh"
set "delim=#"
set "count=0"

:loop
call:finddelims
if defined str goto:loop
echo.%count%
goto:eof

:finddelims
for /f "tokens=1,* delims=%delim%" %%a in ("%str%") do (
  echo.%%a
  set /a "count+=1"
  set "str=%%b"
)

Here I chose the symbol # as a separator, the string was set in the variable str with an arbitrary string with separators.
The double delimiter counts as 1. The delimiter at the end of the line is ignored.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question