JS implementation method to determine whether a string variable contains a certain string
varCts = "bblText";if(Cts.indexOf("Text") > 0 ){ alert('Cts contains Text string');}Usage of indexOf:
Returns the character position of the first substring in the String object.
strObj.indexOf(subString[, startIndex])
parameter
strObj
Required option. String object or text.
subString
Required option. The substring to look for in the String object.
starIndex
Optional. This integer value indicates the index that begins searching within the String object. If omitted, look up from the beginning of the string.
illustrate
The indexOf method returns an integer value indicating the start position of the substring inside the String object. If no substring is found, return -1.
If startindex is a negative number, startindex is treated as zero. If it is larger than the largest character position index, it is regarded as the largest possible index.
Perform a lookup from left to right. Otherwise, the method is the same as lastIndexOf.
Example
The following example illustrates the usage of the indexOf method.
function IndexDemo(str2){ var str1 = "BABEBIBOBUBABEBIBOBU" var s = str1.indexOf(str2); return(s); }Ignore case for indexOf for JavaScript
The indexOf function method in JavaScript returns an integer value indicating the start position of the substring inside the String object. If no substring is found, return -1. If startindex is a negative number, startindex is treated as zero. If it is larger than the largest character position index, it is regarded as the largest possible index.
The indexOf function executes a lookup from left to right. Otherwise, the method is the same as lastIndexOf.
The following example illustrates the usage of the indexOf function method.
functionIndexDemo(str2){ varstr1 = "BABEBIBOBUBABEBIBOBU" vars = str1.indexOf(str2); return(s);}The above JS method to determine whether a string variable contains a certain string is all the content I have shared with you. I hope you can give you a reference and I hope you can support Wulin.com more.