Capturing string Functions in JS

Source: Internet
Author: User

Intercepting string functions in Web effects

function Substrdemo () {
var s, SS; Declare a variable.
var s = "The rain in Spain falls mainly in the plain.";
SS = S.substr (12, 5); Gets the substring.
return (SS); Returns "Spain".
}


b = a.substring (start, end);

Start indicates the starting position, if the position is the first one, fill in 0

End indicates termination position


Slice (), the first parameter represents the start position, the second parameter represents the next position in the end position, the length of which is the difference between the second argument and the first parameter, and the value of the string to positive if the parameter value is negative; if the first argument equals greater than the second argument, An empty string is returned.

SUBSTRING (), the first parameter represents the start position, the second parameter represents the next position in the end position, and if the argument value is a negative number, the value is converted to 0 two, and the smaller value is taken as the starting position, and the length of the truncated string is the difference between the larger value and the smaller value.

SUBSTR (), the first parameter represents the start position, and the second parameter represents the length of the Intercept.

Let's take a look at an example (note: The position of the string starts at 0):

<script language= "Web Effects" >
var stmp = "rcinn.cn";

Use a parameter
Alert (Stmp.slice (3));//starts with the 4th character, intercepts to the last character; returns "Nn.cn"
Alert (stmp.substring (3));//starts with the 4th character, intercepts to the last character; returns "Nn.cn"

Use two parameters
Alert (Stmp.slice (1,5))//begins with the 2nd character, to the 5th character; returns "Cinn"
Alert (stmp.substring (1,5)), starting with the 2nd character, to the 5th character, and returning "Cinn"

If only one argument is used and 0, then the entire argument is returned.
Alert (Stmp.slice (0));//returns the entire string
Alert (stmp.substring (0));//returns the entire string

Returns the first character

Alert (Stmp.slice (0,1));//return "R"
Alert (stmp.substring (0,1));//return "R"

In the example above 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 you can see that slice (2,-5) is actually slice (2,3), minus 5 plus string length 8 to positive 3 (if the first digit equals or is greater than the second digit, an empty string is returned), and substring (2,-5) is actually substring (2,0), negative numbers convert to 0,substring always take a smaller number as the starting position.

Alert (stmp.substring (1,5))//begins with the 2nd character, to the 5th character; returns "Cinn"
Alert (STMP.SUBSTR (1,5));//starts with the 2nd character, intercepts 5 characters, and returns "Cinn."

</script>


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.
}

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.