Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery String Contains Manipulation?

In most languages like C# for example given a string you can test (boolean) if that string contains another string, basically a subset of that string.

string x = test2;   if(x.contains("test"))     // do something 

How can I do this in a simple way with Javascript/Jquery?

like image 907
aherrick Avatar asked Apr 28 '09 23:04

aherrick


1 Answers

This is done with indexOf, however it returns -1 instead of False if not found.

Syntax
string.indexOf(searchValue[, fromIndex])

Parameters
searchValue - A string representing the value to search for.
fromIndex - The location within string to start the search from. It can be any integer between 0 and the length of string. The default value is 0.

Return
The first index in string at which the start of the substring can be found, or -1 if string does not contain any instances of the substring.

like image 163
Paolo Bergantino Avatar answered Sep 19 '22 11:09

Paolo Bergantino