String method contains()
Closed this issue · 11 comments
One issue that always comes up in my classes is the need to check if a string contains a string.
While we have the methods startsWith()
and endsWith()
, we're missing a method to check if a string is contained in a string.
I suggest we add a method contains()
.
Yes it should. Can be done with indexOf
:
var basil = "basil";
if(basil.indexOf("sil") !== -1 ){
$.writeln("found sil")
}
if(basil.indexOf("foo") !== -1 ){
$.writeln("found foo")
}
Yes, perfect. And should we call it contains()
or includes()
. I would prefer contains()
, so it would not get mixed up with the JS ES6 .includes()
. But if you guys prefer otherwise, I am open for alternatives.
+1 for contains
There is also a array contains function in es5 could be cool to make this work with strings and arrays
I was wrong it is also Array.includes
polyfill link. Still contains(thing: array|string|object /*non recursive*/): boolean
would be cool
So you mean one single function that works for all these data types, right?
Maybe. 🤔 Don't you think it would be useful ? Objects would be with hasOwnProperty
Array would be useful for sure, object probably once in a while. But sure, no harm in including them.
Hm. I think object is ambiguous. Should it look for the key
or the value
?
And actually is a shallow check not of much use. So maybe we leave object out.
Yes, sounds better somehow.
Also, startsWith and endsWith could then also be changed to work for arrays as well. Not sure, if anyone ever needs that, but at least all three methods would work consistently then. 😛