Ways to intercept strings in JavaScript

Source: Internet
Author: User

Method:

1.substring (Str,end)

STR is required to be entered and must be positive;

End is optional and must be positive;

by literal means, Str is the starting position of the interception, and the first character position of the string is 0;end as the end position of the intercept.

The substring returned by the substring () method includes the character at start, but does not include the character at the end.

Cases

The code is as follows Copy Code

<meta charset= "UTF-8"/>
<script type= ' Text/javascript ' >
/**
* Substring function using demo
*/
var str = ' Welcome to all children's shoes to June-Tai blog guest ';
var sub = str.substring (3);
Alert (sub); Out: Children's shoes to the Jones Taiwan blog Guest
</script>

Cases

The code is as follows Copy Code

<meta charset= "UTF-8"/>
<script type= ' Text/javascript ' >
/**
* Substring function using demo
*/
var str = ' Welcome to all children's shoes to June-Tai blog guest ';
var sub = str.substring (3,11);
Alert (sub); Out: Children's shoes to the Jones station blog
</script>

Substr

SUBSTR also requires at least one argument, the first parameter is the starting position, the second is optional, and is the length.

Only one parameter

The code is as follows Copy Code

<meta charset= "UTF-8"/>
<script type= ' Text/javascript ' >
/**
* Substring function using demo
*/
var str = ' Welcome to all children's shoes to June-Tai blog guest ';
var sub = str.substr (3);
Alert (sub); Out: Children's shoes to the Jones Taiwan blog Guest
</script>

Two parameters

The code is as follows Copy Code

<meta charset= "UTF-8"/>
<script type= ' Text/javascript ' >
/**
* Substring function using demo
*/
var str = ' Welcome to all children's shoes to June-Tai blog guest ';
var sub = str.substr (3,2);
Alert (sub); Out: Child
</script>

Comprehensive example

The number of digits in the string is starting at 0.

The code is as follows Copy Code
<script language= "JavaScript"
var stmp = "rcinn.cn";
////Use a parameter
alert (Stmp.slice (3));//start with the 4th character, intercept to the last character; nn.cn "
Alert (stmp.substring (3));//starts with the 4th character, intercepts to the last character, returns" nn.cn "
//uses two parameters
Alert (stmp.slice (1,5))// Starts with the 2nd character, to the 5th character, returns the "Cinn"
Alert (1,5);//starts with the 2nd character, to the 5th character, returns "Cinn"
///If only one argument is used and 0, then returns the entire argument
Alert (stmp.slice (0));//returns the entire string
alert (stmp.substring (0));//returns the entire string
//That how to return only the first character, you can use other functions, So if you have to use both methods, specify that the first argument is 0, and the second argument is 1, see the example below
Alert (Stmp.slice (0,1));//Return "R"
Alert (stmp.substring (0,1));//return "R "
//In the above example we can see that the use of slice () and substring () is the same, and the return value is the same, but when the argument is negative, their return value is not the same, look at the following example
Alert (Stmp.slice (2,-5)) ;//return "I"
Alert (stmp.substring (2,-5));//return "RC"
//from the above two examples, we can see that slice (2,-5) is actually slice (2,3), minus 5 is converted to positive 3, and substring ( 2,-5) is actually substring (2,0), and negative numbers are converted to 0,swubstring always take the smallest number as the starting position.
</script>

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.