JavaScript indexof Function Usage Description _ Basics
Source: Internet
Author: User
How to use: Strobj.indexof (str,startindex[optional])
Program code
Where Strobj is a required option. A String object or text.
STR is a must option. The substring to find in the string object.
startindex is optional. The integer value indicates where to start the lookup within a String object, starting at 0. If omitted, it is looked up from the beginning of the string.
Note: The indexof of JavaScript is case-sensitive.
The IndexOf function method in JavaScript returns an integer value that indicates the starting position of the substring of a string object. If no string is found, returns-1. If the startindex is a negative number, the startindex is treated as zero. If it is larger than the maximum character position index, it is considered to be the largest possible index.
The IndexOf function is to perform a lookup from left to right
The following example illustrates the use of the IndexOf function method.
Program code
var str1= "FDIEJDIFADF";
var str= "E";
var i=str1.indexof (str);
alert (i);
In front of said, IndexOf is the size of the write, sometimes this gives us a certain amount of trouble, then how to solve it? Of course, the easiest way to do this is to convert the characters to uppercase or lowercase with tolowercase or touppercase.
The code is as follows:
The following method extends the indexof (from the network) using regular
Program code
<script>
String.prototype.indexOf = function (f,m) {
var mm = (M = = False)? "I": "";
var re = eval ("/" + F + "/" +mm);
var RT = This.match (re);
return (RT = null)? -1:rt.index;
}
var test = "Absnegkiugfkalg";
Alert (Test.indexof ("Kiu", false));
</script>
The following extension is more powerful, it is compatible with the original IndexOf function, and can also be ignored size lookup (also from the network).
Program code
<script language= "JavaScript" >
String.prototype._indexof = String.prototype.indexOf;
String.prototype.indexOf = function ()
{
if (typeof (Arguments[arguments.length-1])!= ' Boolean ')
Return this._indexof.apply (this,arguments);
Else
{
var bi = arguments[arguments.length-1];
var thisobj = this;
var idx = 0;
if (typeof (Arguments[arguments.length-2]) = = ' number ')
{
IDX = arguments[arguments.length-2];
Thisobj = this.substr (idx);
}
var re = new RegExp (arguments[0],bi? ') I ': ');
var r = Thisobj.match (re);
return r==null?-1:r.index + idx;
}
}
Alert ("Bcssssasdfsdf". IndexOf (' A ', 3,true));
Alert ("Bcssssasdfsdf". IndexOf (' A ', 3));
</script>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.