'Is the empty string a subtring of all strings? [duplicate]
I've been designing a search bar function that removes results that do not include the value of the user input. So far it's been successful, but when the user enters an empty string, I want it to return back to default and reverse all of the hidden posts. And while it does work, I wanted to ask the question if all strings contain a substring "" which is empty as it would help my understanding.
Here is what I mean:
let strArray = ['Hello', 'w', '324551', ''];
for (var i=0; i<strArray.length; i++) {
console.log(strArray[i].indexOf("") !== -1);
}
Output:
True
True
True
True
This may be a simple question to answer for some, but I'd just like clarification to reason my work.
Solution 1:[1]
Yes it is. From the spec:
- If searchValue is the empty String and
fromIndex ? len, return fromIndex.
Where fromIndex is almost always 0. (It will only not be 0 if you pass a second optional parameter to indexOf)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | CertainPerformance |
