How to get the substring of a specific string

Hello! How do I get the substring of a specific string. Let’s say, for example, that the full string is 123456789.

  • I’ve been able to use variable.toString().substring(0,3) to pull 123 from the string.
  • I can’t seem to figure out how to get 45 from 123456789. I tried variable.toString().substring(3,2) and variable.toString().substring(4,2), but neither of them are pulling what I need. When I use (3,2), it pulls only the 3rd character from the string, the 3. When I use (4,2), it pulls the 3rd and 4th characters, the 3 and 4. When I use (5,2), I get the 3rd, 4th, and 5th characters, the 3, the 4, and the 5.
  • I’ve been able to use variable.toString().substring(5) to pull 6789 from the string.

What am I doing wrong for the middle piece? Thank you in advance!

I posted this question here in the event so that someone else needs the same answer, they can easily get it from the forum. Thanks!

1 Like

Hello @BrandonTerry This should help i guess variable.toString().substring(3,5)
Let me know if it works :slightly_smiling_face:

1 Like

Hi @Kudlappa_Gouder!

I was able to get it with variable.toString().substring(5,3) and with your suggestion of variable.toString().substring(3,5).

This one stumps me because I would’ve thought that it would be variable.toString().substring(3,2), with 3 meaning to start on the 4th character (since it starts at 0) and 2 meaning to pull the two characters.

I checked W3Schools, and it shows this as its template: string.substring(start, end), which only makes sense that it should be (3,2). Weird!

Okay, I just read something that is confusing but, at the same time, it kind of helps me understand. W3 states “If start is greater than end, parameters are swapped.” Cool, but weird!

Anyway, thank you for your help!

2 Likes