Friday, February 23, 2024
 Popular · Latest · Hot · Upcoming
158
rated 0 times [  164] [ 6]  / answers: 1 / hits: 86268  / 15 Years ago, tue, april 28, 2009, 12:00:00

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?


More From » jquery

 Answers
21

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.


[#99635] Thursday, April 23, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rhett

Total Points: 671
Total Questions: 100
Total Answers: 102

Location: Hong Kong
Member since Tue, Oct 19, 2021
2 Years ago
rhett questions
Thu, Oct 28, 21, 00:00, 2 Years ago
Mon, Aug 2, 21, 00:00, 3 Years ago
Fri, Dec 11, 20, 00:00, 3 Years ago
Fri, Aug 7, 20, 00:00, 4 Years ago
Thu, Mar 26, 20, 00:00, 4 Years ago
;