ECMAScript 6+ (ES6+) – Using Searching strings
ECMAScript 6, also known as ES6 or ES2015, brought many new features and improvements to JavaScript, making it more powerful and easier to use. One of the enhancements in ES6 is the ability to search for strings more efficiently using the new methods introduced.
One of the most commonly used methods for searching strings in ES6 is the includes() method. This method allows you to check if a string includes another string, returning true or false based on the presence of the specified string. Here’s an example:
function flashify_checkString(str, searchStr) { return str.includes(searchStr); }
This flashify_checkString() function takes two parameters – str (the string to search in) and searchStr (the string to search for). It then uses the includes() method to check if searchStr is present in str and returns true if it is, and false if it isn’t.
Another useful method for searching strings in ES6 is the startsWith() method. This method allows you to check if a string starts with another specified string, returning true or false accordingly. Here’s an example:
function flashify_startsWith(str, searchStr) { return str.startsWith(searchStr); }
In the flashify_startsWith() function, the startsWith() method is used to determine if str starts with searchStr. If it does, the function returns true; otherwise, it returns false.
ES6 also introduced the endsWith() method, which checks if a string ends with a specified string, returning true or false based on the result. Here’s an example of how you can use it:
function flashify_endsWith(str, searchStr) { return str.endsWith(searchStr); }
The flashify_endsWith() function takes str as the string to check and searchStr as the string to look for at the end of str. It then uses the endsWith() method to return true if str ends with searchStr, and false if it doesn’t.
These ES6 string searching methods provide a more efficient and convenient way to search for specific strings within other strings, making JavaScript development easier and more streamlined.