The example in this article describes how JS determines the content of a string. Share it with everyone for your reference. The details are as follows:
1. Example:
var tempStr = "tempText" ; var bool = tempStr.indexOf("Texxt"); //返回大于等于0的整数值,若不包含"Text"则返回"-1。 if(bool>0){ document.write("包含字符串"); }else{ document.write("不包含字符串"); }
2. indexOf usage:
strObj.indexOf(subString[, startIndex])
The indexOf function method in JavaScript returns an integer value indicating the starting position of the substring within the String object. If the substring is not found, -1 is returned. If startindex is negative, startindex is treated as zero. If it is greater than the largest character position index, it is treated as the largest possible index.
Parameters:
strObj: required, String object or text.
subString: Required, the substring to be found in the String object.
starIndex: Optional, this integer value indicates the index within the String object to start searching. If omitted, search from the beginning of the string;
If startindex is negative, startindex is treated as zero. If it is greater than the largest character position index, it is treated as the largest possible index.
3. The difference with lastIndexOf:
ThelastIndexOf() method retrieves the substring starting from the end of the string.
I hope this article will be helpful to everyone’s JavaScript programming design.