Home > Web Front-end > JS Tutorial > body text

JS method to determine whether a string contains_javascript skills

WBOY
Release: 2016-05-16 16:01:03
Original
2850 people have browsed it

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("不包含字符串");
}
Copy after login

2. indexOf usage:

strObj.indexOf(subString[, startIndex])
Copy after login

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:

The

lastIndexOf() method retrieves the substring starting from the end of the string.

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template