Javascript Split method, indexOf method, lastIndexOf method, and substring Method

Source: Internet
Author: User

SplitMethod:
The following example demonstrates how to convert the split and integer strings...
<Script>
Function edevil ()
{
Var toint = parseInt ("123"); // convert the string to an integer
Var intvalue = 123;
Var tostr = intvalue. toString (); // type conversion to string
Var my_friends = "test1, test2, test3, test4, test5 ";
Var friend_array = my_friends.split (",");
For (loop = 0; loop <friend_array.length; loop ++)
{
Document. writeln (friend_array [loop] + "is myfriend. <br> ");
}
}
IndexOf method:
Returns the first occurrence of a substring in a String object.
StrObj. indexOf (subString [, startIndex])
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 it is omitted, It is searched from the beginning of the string.
Description
The indexOf method 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.
Search from left to right. Otherwise, this method is the same as lastIndexOf.
Example
The following example illustrates how to use the indexOf method.
Program code
Copy codeThe Code is as follows:
Function IndexDemo (str2 ){
Var str1 = "BABEBIBOBUBABEBIBOBU"
Var s = str1.indexOf (str2 );
Return (s );
}

LastIndexOf method:
Returns the position where the substring of the String object appears.
StrObj. lastIndexOf (substring [, startindex])
Parameters
StrObj
Required. String object or text.
Substring
Required. The substring to be searched in the String object.
Startindex
Optional. This integer indicates the starting index position of the String object. If omitted, the search starts from the end of the string.
Description
The lastIndexOf method 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.
Search from right to left. Otherwise, the method is the same as indexOf.
The following example illustrates how to use the lastIndexOf method:
Program code
Copy codeThe Code is as follows:
Function lastIndexDemo (str2)
{
Var str1 = "BABEBIBOBUBABEBIBOBU"
Var s = str1.lastIndexOf (str2 );
Return (s );
}

Substring method:
Returns the substring at the specified position in the String object.
Program code
StrVariable. substring (start, end)
"String Literal". substring (start, end)
Parameters
Start
Specifies the starting position of the substring. The index starts from 0.
End
Specifies the end position of the substring. The index starts from 0.
Description
The substring method returns a string that contains a substring from start to the end (excluding end.
The substring method uses a small value in start and end as the starting point of the substring. For example, strvar. substring (0, 3) and strvar. substring (3, 0) return the same substring.
If start or end is NaN or negative, replace it with 0.
The length of the substring is equal to the absolute value of the difference between start and end. For example, the length of the substring returned in strvar. substring (0, 3) and strvar. substring (3, 0) is 3.
Example
The following example demonstrates the usage of the substring method.
Program code
Copy codeThe Code is as follows:
Function SubstringDemo (){
Var ss; // declare the variable.
Var s = "The rain in Spain falls mainly in the plain ..";
Ss = s. substring (12, 17); // obtain the substring.
Return (ss); // returns a substring.
}

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.