Javascript is used to determine whether a string contains a string and indexOf

  • 2020-03-26 21:28:38
  • OfStack

Through the determination of the specified directory to achieve the display of advertising

 
if(location.href.indexOf("//www.jb51.net/codes/")>-1){ 
alert('ok'); 
} 


 
var Cts = "bblText"; 
if(Cts.indexOf("Text") >= 0 ) 
{ 
alert('Cts Contained in the Text string '); 
} 

IndexOf usage:

Returns the character position of the first occurrence of a substring in a String object.

StrObj. IndexOf (subString [startIndex])

parameter

strObj

Will be options. String object or literal.

The subString

Will be options. The substring to look for in the String object.

starIndex

Optional. The integer value indicates the index in the String object for which the search is started. If omitted, look at the beginning of the string.

instructions

The indexOf method returns an integer value indicating the starting position of the substring inside 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 largest character position index, it is considered the largest possible index.

Perform the lookup from left to right. Otherwise, the method is the same as lastIndexOf.

The sample

The following example illustrates the use of the indexOf method.
 
function IndexDemo(str2){ 
var str1 = "BABEBIBOBUBABEBIBOBU" 
var s = str1.indexOf(str2); 
return(s); 
} 

IndexOf for JavaScript ignores case

The indexOf function method in JavaScript returns an integer value indicating the starting position of the substring inside 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 largest character position index, it is considered the largest possible index.
The indexOf function performs a lookup from left to right. Otherwise, the method is the same as lastIndexOf.

The following example illustrates the use of the indexOf function method
The same code at the page code block index 2


Related articles: