JavaScript Split method, IndexOf method, LastIndexOf method and substring Method _ basics

Source: Internet
Author: User
SplitMethod:
The following program example implements the use of Split and integer string interchange ...
<script >
function Evil ()
{
var toint=parseint ("123");//string converted to plastic
var intvalue=123;
var tostr=intvalue.tostring ();//type converted 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 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
Copy Code code as follows:

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
Copy Code code as follows:

function Lastindexdemo (STR2)
{
var str1 = "Babebibobubabebibobu"
var s = str1.lastindexof (STR2);
return (s);
}

substring Method:
Returns a substring at the specified position in a string object.
Program code
Strvariable.substring (start, end)
"String Literal". Substring (start, end)
Parameters
Start
Indicates the starting position of the substring, starting at 0.
End
Indicates the end position of the substring, starting at 0.
Description
The substring method returns a string containing a substring from start to the last (not including end).
The substring method uses the smaller values in both start and end as the starting point for substrings. For example, strvar.substring (0, 3) and strvar.substring (3, 0) will return the same substring.
If start or end is NaN or negative, replace it with 0.
The length of the substring equals 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 shows the use of the substring method.
Program code
Copy Code code as follows:

function Substringdemo () {
var SS; Declare a variable.
var s = "The rain in Spain falls mainly in the plain ...";
SS = S.substring (12, 17); Takes a 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.