This article mainly introduces how to judge string inclusion in javascript. It can effectively detect whether a string contains fixed characters or substrings. It involves the use of indexOf in javascript and is very simple and practical, for more information about how to use JavaScript to determine string inclusion, see the example in this article. Share it with you for your reference. The details are as follows:
1. Example:
Var tempStr = "tempText"; var bool = tempStr. indexOf ("Texxt"); // returns an integer greater than or equal to 0. If "Text" is not included, "-1 is returned. If (bool> 0) {document. write ("contains string");} else {document. write ("does not contain string ");}
2. indexOf usage:
strObj.indexOf(subString[, startIndex])
The indexOf Function Method in JavaScript returns an integer indicating the starting position of the substring in the String object. If no substring is found,-1 is returned. If startindex is negative, startindex is treated as zero. If it is larger than the index at the largest character location, it is regarded as the largest possible index.
Parameters:
StrObj: required, String object or text.
SubString: required. The subString to be searched in the String object.
StarIndex: Optional. This integer indicates the index in the String object. If omitted, it will be searched from the beginning of the string;
If startindex is negative, startindex is treated as zero. If it is larger than the index at the largest character location, it is regarded as the largest possible index.
3. Differences from lastIndexOf:
The lastIndexOf () method is used to retrieve substrings from the end of a string.
I hope this article will help you design javascript programs.