FreeCodeCamp and JavaScript: the substr() Method

We’re going to take a look at the substr() method in this post.  Basically, it takes the characters in a string from a starting position through (inclusive of) and ending position and returns it to you.

Per MDN, here’s the syntax:

str.substr(start[, length]);

As you can see, there are two parameters:

  • start – This tells the function where to start extracting the characters.  If you give it a negative number, it will start that many characters from the end of the string and then proceed forwards.
  • length – this is optional and signifies the number of characters to retrieve from the string.

As always, the first character in the string is 0, and the end character is str.length – 1.  If you specify more characters than the string has, substr() will just stop at the end of the string and return whatever it has collected so far.  If you specify a location to start that is past the end of string, you’ll get an empty string back.  If you specify a negative value, as mentioned above, it will start that many characters back from the end of the string; however, if you specify a negative number greater than the length of the string, it will start at 0 (the first character in the string).  Note that negative values don’t work with Microsoft JScript (what a surprise); see MDN for a way to work around this if you need it.

If you specify a length of 0 or less, you’ll get an empty string back, and if you don’t specify a length at all, you get from the starting character to the end of the string back.

Let’s look at MDN’s examples:

var str = ‘abcdefghij’;

We’re going to use the above string for all of the examples.  The first example starts at the second character in the string (remember, the count starts at 0, not 1) and returns 2 characters.

console.log(‘(1, 2): ‘ + str.substr(1, 2)); // ‘(1, 2): bc’

The second example starts 3 back from the end of the string – that’s h – and returns 2 characters as specified.

console.log(‘(-3, 2): ‘ + str.substr(-3, 2)); // ‘(-3, 2): hi’

The next example 3 starts 3 back from the end of the string – that’s h – and returns the rest of the string since no end character is specified.

console.log(‘(-3): ‘ + str.substr(-3)); // ‘(-3): hij’

This example starts at character 1 (the second character in the string) and, since no end character is specified, returns the rest of the string.

console.log(‘(1): ‘ + str.substr(1)); // ‘(1): bcdefghij’

The next example starts 20 back from the end and returns 2 characters.  Now, since the string is only 10 characters long, substr() starts at the first character and returns it and the second character.

console.log(‘(-20, 2): ‘ + str.substr(-20, 2)); // ‘(-20, 2): ab’

Finally, we tell substr() to start at the twentieth character and return 2 characters.  Obviously, this is way beyond the end of the string, so we get nothing back.

console.log(‘(20, 2): ‘ + str.substr(20, 2)); // ‘(20, 2): ‘

Advertisements

One thought on “FreeCodeCamp and JavaScript: the substr() Method

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s