- Modern JavaScript Web Development Cookbook
- Federico Kereki
- 78字
- 2021-07-02 14:49:53
Searching in strings
There are new functions to determine whether a strings starts with, ends with, or includes a given string. This can give you much relief from using indexOf(...) and length-related calculations:
"Hello, there!".startsWith("He"); // true
"Hello, there!".endsWith("!"); // true
"Hello, there!".includes("her"); // true
Each of these methods has a position as an optional second parameter, which specifies where to do the search; see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith, and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes for more information.