Javascript indexof Function

Source: Internet
Author: User

Usage: strobj. indexof (STR, startindex [Optional])
ProgramCode
Strobj is required. String object or text.
STR is required. The substring to be searched in the string object.
Startindex is optional. This integer indicates the position where the string object starts searching, starting from 0. If it is omitted, It is searched from the beginning of the string.

Note: javascript indexof is case sensitive.
The indexof Function Method in Javascript returns an integer indicating the starting position of the substring in the string object. If no string 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.
The indexof function performs search from left to right.
The following example illustrates how to use the indexof function method.
Program code
VaR str1 = "fdiejdifadf ";
VaR STR = "E ";
VaR I = str1.indexof (STR );
Alert (I );

As mentioned above, indexof is a write with different sizes. Sometimes this causes us some trouble. How can we solve this problem ?? Of course, the simplest way is to convert the characters into uppercase or lowercase letters using tolowercase or touppercase.
The Code is as follows:
Program code
<SCRIPT>
VaR STR = 'abcdef ';
VaR str1 = 'bcd ';
Alert (Str. tolowercase (). indexof (str1.tolowercase ()));
Str2 = 'abcdef ';
Alert (str2.tolowercase (). indexof (str1.tolowercase ()));
</SCRIPT>

the following method uses regular expressions to expand indexof (from the network)
program code

The following extension is more powerful. It is compatible with the original indexof function and can also be used to ignore the size (also from the network ).
program code

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.