So you may want to rewrite your code as follows: function contains(a, obj) )) //falseĬonsole.log(. How to check if a string contains a substring in JavaScript Option 1: Use the includes Method (ECMAScript 6) Option 2: Use the indexOf Method (ECMAScript 5. Does anyone know of a way to check if a list contains a string without using indexOf In my array some strings can contains parts of others, so indexOf will produce false positives. If you need the best performance, benchmark for yourself in the relevant execution environments.Īs others have said, the iteration through the array is probably the best way, but it has been proven that a decreasing while loop is the fastest way to iterate in JavaScript. Javascript list contains a string duplicate Closed 6 years ago. includes ( searchvalue, start) Parameters Return Value More Examples Start at position 12: let text 'Hello world, welcome to the universe.' let result text. The includes () method is case sensitive. If you need an easy solution, look for other answers. The includes () method returns true if a string contains a specified string. ![]() It might not be relevant to modern JS execution contexts. The promised performance improvement was based on a benchmark done in browsers of that time. Update from 2019: This answer is from 2008 (11 years old!) and is not relevant for modern JS usage.
0 Comments
Leave a Reply. |