How to count only quotes/characters in a string

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jeb
Expert
Posts: 830
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

How to count only quotes/characters in a string

#1 Post by jeb » 05 Jun 2012 08:17

Sometimes it's useful to count a single character in a string, and not all characters like stringLength function do.

There is the obvious solution to iterate over the complete string and examine each character, but I don't like this brute force way.
So I decide to create an other more elegant solution.

The core idea is to replace rearrange the string so that each quote (or the character you want) will be replaced with the string +1,
and all other characters will be completly removed.

For this I use a technic, I called it delayed reduction, to do the magic work.

The sample string
The dog said : "Hello you", the cat answered: "Hi" and smile
will be replaced to
-1!!#:X=The dog said : !!#:X=Hello you!!#:X=, the cat answered: !!#:X=Hi!!#:X= and smile!

The content of the variable # is +1, so after the exclamation marks are expanded the line is reduced to
-1+1+1+1+1+1

And this can be easily calculated with SET/a to 4.

The rest of the code is only to avoid problems with special characters like exclamation marks and carets in the string.

Code: Select all

:::::::::::::::::::::::::::::::::::::::::::
:CountQuotes <stringVar> <result>
setlocal EnableDelayedExpansion
set "line=!%~1!"
set "#=+1"

rem DelayedExpansion: double all quotes
set "line=!line:"=""!"

rem DelayedExpansion: remove all carets ^
set "line=!line:^=!"

rem PercentExpansion: Remove all !
set "line=%line:!=%"

rem PercentExpansion: Replace double quotes to !!#:#=
set "line=-1^!#:#=%line:""=^!^!#:#=%"

for /F "delims=" %%X in ("!line!") do (
   set /a count=%%X!
)

(
   endlocal
   set %~2=%count%
   exit /b
)


jeb

dbenham
Expert
Posts: 1961
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to count only quotes/characters in a string

#2 Post by dbenham » 05 Jun 2012 10:51

:shock: That is insane jeb :lol:

You have a trivial bug, you need to change "temp" into "line" near the top.

I suppose you could add some additional code to specify the character to count. You would need to encode characters like ! and ^ instead of removing them. My guess is the only character you couldn't count would be =.

I'm wondering if something like the following might be faster and easier to code and understand. It replaces each quote with a linefeed and uses FOR /F to count the number of lines. Like your code, there is more work to be done to parametize the char to be counted. And it has the same inability to count =.

Code: Select all

:CountQuotes
setlocal enableDelayedExpansion
set lf=^


set "line=x!%~1!"
for %%C in ("!lf!") do set line=!line:"=%%~Cx!
set count=-1
for /f "delims=" %%A in ("!line!") do set /a count+=1
(
  endlocal
  set %~2=%count%
  exit /b
)

Edit
What about simply using the strLen function to get the string length before and after replacing the char (quote) with nothing? This would have better length restrictions since the string doesn't have to expand to count the chars.

I just realized that there isn't a fast pure batch solution that is case sensitive. (At least I don't think there is)


Dave Benham

jeb
Expert
Posts: 830
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: How to count only quotes/characters in a string

#3 Post by jeb » 05 Jun 2012 13:17

Thanks Dave,

but your solution is also a good idea :D.

You inspire me to create another solution with linefeeds, but without a for loop. :)

Code: Select all

:::::::::::::::::::::::::::::::::::::::::::
:CountQuotes <stringVar> <result>
setlocal EnableDelayedExpansion
set "line=!%~1!"
set "line=!line:%%=!"
for %%L in ("!LF!") DO set "line=!line:"=%%~Lset /a cnt+=1%%~Lrem # !"
set cnt=0
ECHO ON
(
rem # %line%
)
@ECHO OFF
(
   endlocal
   set %~2=%cnt%
   exit /b
)


jeb

dbenham
Expert
Posts: 1961
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to count only quotes/characters in a string

#4 Post by dbenham » 05 Jun 2012 14:53

Very interesting. This last one may be the fastest, but it expands the string the most, so it limits the size of the string that can be processed.

You need to protect against the input being undefined.

I don't see the need to remove %% from the string.

And I assume the ECHO ON/OFF is just to help with understanding.

Here is a variation that minimizes the growth of the string.

Code: Select all

:::::::::::::::::::::::::::::::::::::::::::
:CountQuotes <stringVar> <result>
setlocal EnableDelayedExpansion
set lf=^


set "line=!%~1!"
if defined line for %%L in ("!LF!") DO set "line=!line:"=%%~Lset/ac+=1%%~L::!"
set c=0
(
  ::%line%
  rem
)
(
  endlocal
  set %~2=%c%
  exit /b
)

Edit
Heck - if we are going to get rid of FOR loops, let's do it for real :D

Code: Select all

:::::::::::::::::::::::::::::::::::::::::::
:CountQuotes <stringVar> <result>
setlocal EnableDelayedExpansion
set "line=!%~1!"
if defined line set "line=!line:"=^

set/ac+=1^

::!"
set c=0
(
 ::%line%
 rem
)
(
  endlocal
  set %~2=%c%
  exit /b
)


Dave Benham

Post Reply