Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Documentation for String Length  (Read 3577 times)

0 Members and 1 Guest are viewing this topic.

QueenSvetlana

  • Guest
Documentation for String Length
« on: June 12, 2017, 10:52:45 AM »
I was on SO and came across this post that asked how to break a string by delimiter and then count how many strings exist. The given answer(at the time) was this:

Code: [Select]
@echo off
set "string=17.09.01.04.03"
set count=0
for %%a in (%string:.= %) do set /a count+=1
echo %count%

Is it documented anywhere that %string:.= % will break the string on the period?

strollin



    Adviser
  • Thanked: 84
    • Yes
  • Certifications: List
  • Computer: Specs
  • Experience: Guru
  • OS: Windows 10
Re: Documentation for String Length
« Reply #1 on: June 12, 2017, 10:58:41 AM »
It won't break the string into sub strings but will only determine the count of the sub strings.

QueenSvetlana

  • Guest
Re: Documentation for String Length
« Reply #2 on: June 12, 2017, 11:10:44 AM »
Thanks the for quick reply  :)

Is there documentation somewhere that says the code %string:.= % will provide the count? I'm just wondering how the person who posted the answer was able to figure it out.

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10

Salmon Trout

  • Guest
Re: Documentation for String Length
« Reply #4 on: June 12, 2017, 01:06:56 PM »
I'm just wondering how the person who posted the answer was able to figure it out.
It's just putting basic batch stuff together. They knew these things:

1. This is an example of batch string replacement: %string:.= % It means "the variable %string%, with each period (dot) changed to a space"

2. The replacement makes 17.09.01.04.03 become 17 09 01 04 03

3. A simple FOR command repeats once for each space-separated token

4. The variable %count% is set to 0 (zero) at the beginning.

5. Each time FOR repeats, the set /a count+=1 adds 1 (one) to the value of %count%

6. At the end, %count% is equal to the number of tokens.


« Last Edit: June 12, 2017, 02:08:41 PM by Salmon Trout »