How to find if an array contains a specific string in JavaScript/jQuery?
You really don't need jQuery for this.
var myarr = ["I", "like", "turtles"];
var arraycontainsturtles = (myarr.indexOf("turtles") > -1);
Hint: indexOf returns a number, representing the position where the specified searchvalue occurs for the first time, or -1 if it never
occurs
or
function arrayContains(needle, arrhaystack)
{
return (arrhaystack.indexOf(needle) > -1);
}
It's worth noting that array.indexOf(..)
is not supported in IE < 9, but jQuery's indexOf(...)
function will work even for those older versions.
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);
How do I determine if a String is an Array?
Assuming you want to support some subset of the Javascript grammar, you can use regular expressions to remove whitespace and scalar literals and then check if what is remaining matches the nested pattern [,[,,,],,,]
.
let remove = [ /\s+/g, /'(\\.|[^'])*'/g, /"(\\.|[^"])*"/g, /\d+/g,];
let emptyArray = /\[,*\]/g;
function stringIsArray(str) {
for (let r of remove) str = str.replace(r, '');
if (str[0] !== '[') return false;
while (str.match(emptyArray)) str = str.replace(emptyArray, '');
return str.length === 0;}
console.log(stringIsArray("'abc'"));console.log(stringIsArray(`['abc', ['def', [123, 456], 'ghi',,],,]`));console.log(stringIsArray(String.raw` ['a"b"c', ["d'e'f", [123, [[ [[["[[[5]]]"]]]]], 456], '\"\'""""',,],,]`));
In Java, is a String an array of chars?
Strings are immutable objects representing a character sequence (CharSequence
is one of the interfaces implemented by String).
Main difference to char arrays and collections of chars: String cannot be modified, it's not possible (ignoring reflection) to add/remove/replace characters.
Internally they are represented by a char array with an offset and length (this allows to create lightweight substrings, using the same char arrays).
Example: ['F','o','o','H','e','l','l','o',' ','W','o','r','l','d'], offset=3, count=5 = "Hello"
.
How do I check whether an array contains a string in TypeScript?
The same as in JavaScript, using Array.prototype.indexOf():
console.log(channelArray.indexOf('three') > -1);
Or using ECMAScript 2016 Array.prototype.includes():
console.log(channelArray.includes('three'));
Note that you could also use methods like showed by @Nitzan to find a string. However you wouldn't usually do that for a string array, but rather for an array of objects. There those methods were more sensible. For example
const arr = [{foo: 'bar'}, {foo: 'bar'}, {foo: 'baz'}];
console.log(arr.find(e => e.foo === 'bar')); // {foo: 'bar'} (first match)
console.log(arr.some(e => e.foo === 'bar')); // true
console.log(arr.filter(e => e.foo === 'bar')); // [{foo: 'bar'}, {foo: 'bar'}]
Reference
Array.find()
Array.some()
Array.filter()
Is string in array?
Just use the already built-in Contains() method:
using System.Linq;
//...
string[] array = { "foo", "bar" };
if (array.Contains("foo")) {
//...
}
Related Topics
Difference Between MVC 5 Project and Web API Project
Differencebetween Bool and Boolean Types in C#
In C# Differencebetween Toupper() and Toupperinvariant()
Don't Stop Debugger at That Exception When It's Thrown and Caught
Passing List<> to SQL Stored Procedure
How to Return Anonymous Type from C# Method That Uses Linq to SQL
Change Values in JSON File (Writing Files)
How to Read Attribute Value from Xmlnode in C#
How to Read Value of a Registry Key C#
Does the Use of the "Async" Suffix in a Method Name Depend on Whether the 'Async' Modifier Is Used
Entity Framework 6 Transaction Rollback
Single App.Config Multi-Project C#
.Net Xml Serialization - Storing Reference Instead of Object Copy