Monday, June 8, 2020

JavaScipt String function and features


String

String Quotes

Strings can be enclosed within either single quotes, double quotes.


Backtick quotes

  • process expression

  • process function

  • multi line


Special characters



Accessing characters

Notes:
The only difference between them is that if no character is found, [] returns undefined, and charAt returns an empty string.

iterate character by character



String length



Changing the case




Searching for a substring

str.indexOf

  • returns the position where the match was found
  • -1 if nothing can be found.

str.lastIndexOf(substr, position)
There is also a similar method str.lastIndexOf(substr, position) that searches from the end of a string to its beginning. It would list the occurrences in the reverse order.

includes

The more modern method str.includes(substr, pos) returns true/false depending on whether str contains substr within. It’s the right choice if we need to test for the match, but don’t need its position

str.startsWith and str.endsWith


No comments:

Post a Comment