One of the more basic functions is
substring which takes a string and returns a specific part of that string.
substring function takes two arguments, The first is the position to start extracting the string, and the second is the position to stop extracting the string.
If I were take a substring from 2 to 4, I would get
va, This is because there is a
v at position 2, a
a at position 3, and then we stop just before position 4.
In code this might look something like this;
You can also leave off the second argument to get the string all the way to the end. For example this code:
Would give the result in
sub being set to
What if the first number is out of range, for example it is a negative number of beyond the last character in the string?
A handy property to use in conjunction with
length. This simply tells us how long the string is, for example:
The index of the last character in a string is the length minus 1 (because we start at zero). This means we can get the last character like this:
substring, so whatever you are programming you are sure to bump into it sooner or later.