The indexof of JavaScript functions

Source: Internet
Author: User
Tags integer

IndexOf Method:

Returns the character position for the first occurrence of a substring within a string object.

Strobj.indexof (Substring[,startindex])

Parameters

Strobj

Required option. A string object or text.

SubString

Required option. The substring to find in the string object.

Starindex

Options available. An integer value that indicates the index in which to start the lookup within a string object. If omitted, it is looked up from the beginning of the string.

Description

The IndexOf method returns an integer value that indicates the starting position of the substring of the string object. If no substring 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.

Performs a lookup from left to right. Otherwise, the method is the same as LastIndexOf.

Example

The following example illustrates the use of the IndexOf method.

Program code

function IndexDemo(str2){
var str1 = "BABEBIBOBUBABEBIBOBU"
var s = str1.indexOf(str2);
return(s);
}

LastIndexOf Method:

Returns the last occurrence of the substring of a string object.

Strobj.lastindexof (Substring[,startindex])

Parameters

Strobj

Required option. A string object or text.

Substring

Required option. The substring to find within the string object.

startindex

Options available. The integer value indicates the starting index position of the lookup within the string object. If omitted, the lookup starts at the end of the string.

Description

The LastIndexOf method returns an integer value that indicates the starting position of the substring of the string object. If no substring 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.

Performs a lookup from right to left. Otherwise, the method is the same as indexof.

The following example illustrates the use of the LastIndexOf method:

Program code

function lastIndexDemo(str2)
{
var str1 = "BABEBIBOBUBABEBIBOBU"
var s = str1.lastIndexOf(str2);
return(s);
}

Case-insensitive Javascriptindexof

<script language=javascript>
/**//*
作者:阿牛(牛昆亮) QQ:273352165 MSN:niukl@hotmail.com
声明:可以免费使用,请您保留此信息
如果您有什么改过,记得告诉我!
*/
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>

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.