Check if string contains a value in array
Try this.
$string = 'my domain name is website3.com';
foreach ($owned_urls as $url) {
//if (strstr($string, $url)) { // mine version
if (strpos($string, $url) !== FALSE) { // Yoshi version
echo "Match found";
return true;
}
}
echo "Not found!";
return false;
Use stristr() or stripos() if you want to check case-insensitive.
How do I determine whether an array contains a particular value in Java?
Arrays.asList(yourArray).contains(yourValue)
Warning: this doesn't work for arrays of primitives (see the comments).
Since java-8 you can now use Streams.
String[] values = {"AB","BC","CD","AE"};
boolean contains = Arrays.stream(values).anyMatch("s"::equals);
To check whether an array of int
, double
or long
contains a value use IntStream
, DoubleStream
or LongStream
respectively.
Example
int[] a = {1,2,3,4};
boolean contains = IntStream.of(a).anyMatch(x -> x == 4);
Check if a string contains any element of an array in JavaScript
Problem lies in the for loop, which only iterates once since return ends the function, cutting off the for loop in the process. So, you can update the code like so to make the function only return once the for loop has been completed .
var arr = ['banana', 'monkey banana', 'apple', 'kiwi', 'orange'];
function checker(value) {
var prohibited = ['banana', 'apple'];
for (var i = 0; i < prohibited.length; i++) {
if (value.indexOf(prohibited[i]) > -1) {
return false;
}
}
return true;
}
arr = arr.filter(checker);
console.log(arr);
How to check if a string contains all the elements of an array in Javascript
Use every() function on the arr and includes() on the str;
Every will return true if the passed function is true for all it's items.
var str = 'apple_mango_banana';
var arr = ['apple','banana'];
var isEvery = arr.every(item => str.includes(item));
console.log(isEvery);
How to check if a string contains text from an array of substrings in JavaScript?
There's nothing built-in that will do that for you, you'll have to write a function for it, although it can be just a callback to the some
array method.
Two approaches for you:
- Array
some
method - Regular expression
Array some
The array some
method (added in ES5) makes this quite straightforward:
if (substrings.some(function(v) { return str.indexOf(v) >= 0; })) {
// There's at least one
}
Even better with an arrow function and the newish includes
method (both ES2015+):
if (substrings.some(v => str.includes(v))) {
// There's at least one
}
Live Example:
const substrings = ["one", "two", "three"];
let str;
// Setup
console.log(`Substrings: ${substrings}`);
// Try it where we expect a match
str = "this has one";
if (substrings.some(v => str.includes(v))) {
console.log(`Match using "${str}"`);
} else {
console.log(`No match using "${str}"`);
}
// Try it where we DON'T expect a match
str = "this doesn't have any";
if (substrings.some(v => str.includes(v))) {
console.log(`Match using "${str}"`);
} else {
console.log(`No match using "${str}"`);
}
Detect if string contains any element of a string array
In additional to answer of @Sh_Khan, if you want match some word from group:
let str:String = "house near the beach"
let wordGroups:[String] = ["beach","waterfront","with a water view","near ocean","close to water"]
let worlds = wordGroups.flatMap { $0.components(separatedBy: " ")}
let match = worlds.filter { str.range(of:$0) != nil }.count != 0
Test if a string contains any of the strings from an array
EDIT: Here is an update using the Java 8 Streaming API. So much cleaner. Can still be combined with regular expressions too.
public static boolean stringContainsItemFromList(String inputStr, String[] items) {
return Arrays.stream(items).anyMatch(inputStr::contains);
}
Also, if we change the input type to a List instead of an array we can use items.stream().anyMatch(inputStr::contains)
.
You can also use .filter(inputStr::contains).findAny()
if you wish to return the matching string.
Important: the above code can be done using parallelStream()
but most of the time this will actually hinder performance. See this question for more details on parallel streaming.
Original slightly dated answer:
Here is a (VERY BASIC) static method. Note that it is case sensitive on the comparison strings. A primitive way to make it case insensitive would be to call toLowerCase()
or toUpperCase()
on both the input and test strings.
If you need to do anything more complicated than this, I would recommend looking at the Pattern and Matcher classes and learning how to do some regular expressions. Once you understand those, you can use those classes or the String.matches()
helper method.
public static boolean stringContainsItemFromList(String inputStr, String[] items)
{
for(int i =0; i < items.length; i++)
{
if(inputStr.contains(items[i]))
{
return true;
}
}
return false;
}
Check if an array of strings contains a substring
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)
Related Topics
Getting a Modified Preorder Tree Traversal Model (Nested Set) into a <Ul>
Back to Previous Page with Header( "Location: " ); in PHP
What Does "Mass Assignment" Mean in Laravel
Send Email with PHPmailer - Embed Image in Body
PHP Function to Build Query String from Array
Laravel Recursive Relationships
Laravel Request::All() Should Not Be Called Statically
You Must Enable the Openssl Extension to Download Files via Https
PHP Objects VS Arrays -- Performance Comparison While Iterating
How to Redirect Stdout to a File in PHP
How to Get Customer Details from an Order in Woocommerce
PHP Technique to Query the Apns Feedback Server
Post Content-Length Exceeds the Limit