jQuery String包含操作? [英] jQuery String Contains Manipulation?
问题描述
在C#这样的大多数语言中,例如给定一个字符串,如果该字符串包含另一个字符串,则可以测试(布尔值),基本上是该字符串的一个子集。
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
如何使用Javascript / Jquery以简单的方式完成此操作?
How can I do this in a simple way with Javascript/Jquery?
推荐答案
这是通过 indexOf ,但如果找不到则返回-1而不是False。
This is done with indexOf, however it returns -1 instead of False if not found.
语法
string.indexOf(searchValue [,fromIndex])
参数
searchValue
-
表示要搜索的值的字符串。
fromIndex
-
string
中的位置以开始搜索。它可以是0到 string
长度之间的任何整数。默认值为0.
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.
返回
字符串中的第一个索引可以找到子字符串开头的code>,如果
string
不包含子字符串的任何实例,则返回-1。
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.
这篇关于jQuery String包含操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!