Strings are arguably one of the most important data types in programming. They’re in nearly every higher-level programming language, and being able to work with them effectively is fundamental for developers to create useful programs.
String Inspection Functions or Substrings
Before ECMAScript 6 we have traditionally used
inedxOf() method to identify strings inside other strings.
ECMAScript 6 includes the following three methods, which are designed to do just that:
includes()method returns true if the given text is found anywhere within the string. If it is not found, it returns false.
startsWith()method returns true if the given text is found at the beginning of the string. If it is not found, it returns false.
endsWith()method returns true if the given text is found at the end of the string. If it is not found, it returns false.
Each methods accept two arguments: the text to search for and an optional index from which to start the search.
When the second argument is provided,
startsWith() start the match from that index while
endsWith() starts the match from the length of the string minus the second argument; when the second argument is omitted,
startsWith() search from the beginning of the string, while
endsWith() starts from the end. In effect, the second argument minimizes the amount of the string being searched.
Here is the JSBIN for the same:
The first six calls don’t include a second parameter, so they’ll search the whole string if needed. The last three calls only check part of the string.
The call to
msg.startsWith("o", 4) starts the match by looking at index 4 of the
msg string, which is the “o” in “Hello”.
The call to
msg.endsWith("o", 8) starts the match at index 4 as well, because the
8 argument is subtracted from the string length (12).
The call to
msg.includes("o", 8) starts the match from index 8, which is the “r” in “world”.
While these three methods make identifying the existence of substrings easier, each only returns a boolean value. If you need to find the actual position of one string within another, use the
includes()methods will throw an error if you pass a regular expression instead of a string.
This stands in contrast to
lastIndexOf(), which both convert a regular expression argument into a string and then search for that string.
The repeat() Method
In languages like Python and Ruby, you can repeat a string as:
"foo" * 3; // "foofoofoo"
That doesn’t work in JS, because
* multiplication is only defined for numbers, and thus
"foo" coerces to the
However, ES6 defines a string prototype method
repeat(..) to accomplish the task. ECMAScript 6 adds a
repeat() method to strings, which accepts the number of times to repeat the string as an argument. It returns a new string containing the original string repeated the specified number of times. For example:
Here is the JSBin for the above: https://jsbin.com/jatekiw/edit?html,console
This method is a convenience function above all else, and it can be especially useful when manipulating text. It’s particularly useful in code formatting utilities that need to create indentation levels, like this:
// indent using a specified number of spaces
// whenever you increase the indent
repeat() call creates a string of four spaces, and the
indentLevel variable keeps track of the indent level.
Then, you can just call
repeat() with an incremented
indentLevel to change the number of spaces.
If you enjoyed reading, make that heart go green! Recommend it to friends and of course, let me know if you have any feedback.
You can also join my slack community.
Do checkout http://www.programmingbuddy.club
Exercises for Practice: (optional)
Create a new String with the text “Join the ‘Happy Learning’ Team in slack” and do the following: For all of these, just use above methods and do a console.log of the test.
- Check if the string starts with “Join”?
- Check if the string starts with “Happy”?
- Check if the string ends with “Learning” ?
- Check if the string ends with “slack” ?
- Check if the string includes “Join Now” ?
- Check if the string includes “Happy Learning”?
- Check if the string starts with “Happy” starting with position 10?
- Check if the string ends with “Learning” starting with position 10?
- Check if the string includes “Happy Learning” starting with position 4?
Create a text output as “Hello…How..are…you..you….you???” using the
repeat() method. You can create and use these constants.
const hello = “Hello”;
const how = “How”
const dot = “.”;
const are = “are”;
const you = “you”;
const question = “?”;