#1 03 Dec 2020 21:46

Unknow0059
Member
Registered: 03 Dec 2020
Posts: 2

How do Variable Substrings work with single negative numbers?

The SS64 page for Variable Substrings confuses me.
page: https://ss64 dot com/nt/syntax-substring dot HTML
Very funny how this forum has the "url=.../url" syntax but not only can I not link URLs, I cannot link site that this forum is based on. Anyway...

The syntax reference and what actually happens differ:

Syntax
      %variable:~num_chars_to_skip%

This is the syntax if you want to only use a single number to extract a substring from the variable.
As per the name of that... parameter I guess? the number you give is the number of characters that will be skipped, and then the rest won't, so they will be extracted.
Negative numbers count from the end of the string.

So, theoretically, if you extracted a positive number from a variable, you would count and skip that number from the start, and the rest would be left.
Similarly, if you extracted a negative number from a variable, you would count and skip that number from the end, and the rest would be left.
For example, assume the variable "a = abcde".
Extracting "1" from it should give us "bcde", because it skips One character counting from the start, since the number is positive.
Extracting "-1" from it should give us "abcd", because it skips One character counting from the end, since the number is negative.

Let's test it.

echo %a:~1%

results in "bcde", as expected.

echo %a:~-1%

results in "e", which is woefully unexpected.

More over, the syntax reference contradicts itself in the "::Extract only the last 7 characters" section:

SET _test=123456789abcdef0
::Extract only the last 7 characters

 SET _result=%_test:~-7%
 ECHO %_result%

What do you mean "Extract"? That is not what the very start of the page says should happen. It says they should be skipped.
The result of this code is "abcdef0". So, it is not skipping them, as the page said originally.
This means that either this concept of "skipping" is wrong, or that the behavior of the code changes depending on whether the number is positive or negative, and the documentation does not make that explicit.

Offline

#2 04 Dec 2020 05:31

T3RRY
Member
Registered: 15 Oct 2020
Posts: 10

Re: How do Variable Substrings work with single negative numbers?

There is nothing unexpected here.

The terms skip and extract are accurate when the appropriate substring "argument" is used

examples:

 Set "string=abcde"
rem extract first four characters
 Echo/%string:~0,4%
rem output abcd

rem Extract all but last character. Note the ,
 Echo/%string:~,-1%
rem output abcd

rem skip first 2 characters extract up to and including the 2nd last character
 Echo/%string:~2,-1%
rem output cd

rem Skip the first character. Note the absence of ,
 Echo/%string:~1%
rem output bcde

rem Extract last character. Note the absence of ,
 Echo/%string:~-1%
rem output e

Last edited by T3RRY (04 Dec 2020 05:34)

Offline

#3 04 Dec 2020 18:22

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,108
Website

Re: How do Variable Substrings work with single negative numbers?

Hi Unknow0059, I made you a member so you can post links now.

You make a good point that the syntax description uses the term 'skipped chars', but thats only true for positive numbers.
For negative numbers it counts back from the end, which is not really what most people think of with 'skipping', so I have just reworded that part of the page.

Offline

#4 05 Dec 2020 01:12

Unknow0059
Member
Registered: 03 Dec 2020
Posts: 2

Re: How do Variable Substrings work with single negative numbers?

I see. By the way pardon if I sounded angry in tone or anything. SS64 has been invaluable for me to learn Batch.

Offline

Board footer

Powered by FluxBB