#Difference between 2 numbers Vs interval between 2 numbers

30 messages · Page 1 of 1 (latest)

jaunty trellis
#

Hello guys, I have a simple question. It's basically related to programming but never mind, I'm interested in how we subtract things.

Consider the following:

string message = "Find what is (inside the parentheses)";

int openingPosition = message.IndexOf('(');
int closingPosition = message.IndexOf(')');


int length = closingPosition - openingPosition;
Console.WriteLine(message.Substring(openingPosition, length));

Here, why length doesn't have the closing parentheses index included?

Is it the same thing when we do for e.g 7 - 4 ? Is there a difference when we say "subtract 2 numbers" and "interval between 2 numbers" ?

sand solarBOT
tiny niche
jaunty trellis
#

is there a way to visualize it, like using a number line ?

#

this confusion arises from length = closingPosition - openingPosition 😭

tiny niche
#

Ok lets take the string kingleo

jaunty trellis
#

yep

tiny niche
#

Lets say we substring from 0 to 3

jaunty trellis
#

king will be the result if both are inclusive

tiny niche
#

Anyways, here's whats going to happen

#

Im going to draw a vertical line directly to the left of the letter at both indices

#

And then i connect the lines to make a box

jaunty trellis
#

yep

tiny niche
#

And my resulting .substring is kin

jaunty trellis
#

yeah

tiny niche
#

So do you see why we dont include the g

jaunty trellis
#

yeah, if we think of it like that, yeah, like if we think of the vertical lines drawn before each index, where each time 0 index will always be included while last index won't be included

#

It's clearer now, thanks ! By the way, this is what happen in real scenario, like the vertical lines being drawn before each index position or it's just on paper?

tiny niche
#

But i assume its because something like substring from index 0 to index 0 doesnt make sense to include a character

jaunty trellis
#

😂 anyway, I used the verical lines logic, it's clearer for me now

tiny niche
#

!done

sand solarBOT
#

If you are done with this channel, please mark your problem as solved by typing .close

jaunty trellis
#

.close