Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if an array of strings contains a substring

I have an array like this:

array = ["123", "456", "#123"] 

I want to find the element which contains the substring "#". I tried array.includes("#") and array.indexOf("#") but it didn't work.

How can I check if any of the strings in this array contain the substring "#"?

like image 451
user7334203 Avatar asked Jun 08 '17 15:06

user7334203


People also ask

How do you check if a string contains text from an array of substrings Python?

To check if a Python string contains the desired substring, you can use the "in" operator or the string. find() method. To get the first index of a substring in a string, you can use the string. index().

How do you find if an array contains a specific string in JavaScript?

JavaScript Array includes() The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found. The includes() method is case sensitive.

How do you check if an array of strings contains a string TypeScript?

Use the includes() Method to Check if a String Is Present in a TypeScript Array. The includes() method determines whether a target element is present in the array. It takes in the target element and returns a boolean value to represent the value in the array.

How do you check if a string contains a substring JavaScript?

The includes() method returns true if a string contains a specified string. Otherwise it returns false . The includes() method is case sensitive.


1 Answers

Because includes will compare '#' with each array element.

Let's try with some or find if you want to find if you want to get exactly element

var array = ["123", "456", "#123"];    var el = array.find(a =>a.includes("#"));    console.log(el)
like image 146
taile Avatar answered Sep 20 '22 09:09

taile