Jump to content

slice( ) - Javascript

0
  Ángel Manuel García Carmona's Photo
Posted Sep 09 2010 08:42 AM

The "slice( )" method is that which extract part of an string and gives an new string, requiring this method a number that indicates the index position and other that indicates the final position being this the total of the last letter number + 1 equal at the real position. The basic structure is the following:

string.slice(beginning, finish)

There are some cases where the structure only is composed of one argument that only indicates the position where we will make the extraction. This is an example:

var url = "http://www.yahoo.es";
document.write(url.slice(7));

In this example, the code will extract the characters that are from the indicated position.

In the following case the code will extract the characters from a position to other, looking for example this code block:

var frase = "La sociedad es el motor del futuro";
document.write(frase.slice(4,10));

In this example, the code will write the characters from an associated position with number 4 to the 10th (it has assigned the number nine but like it's a final argument we will add 1 being indicated 10, the real position), being the written message "ociedad".
Ángel Manuel

Tags:
0 Subscribe


1 Reply

0
  Peter_Kahrel's Photo
Posted Sep 12 2010 04:00 AM

You can also use a negative number so that the last part of a string is returned:

"retrograde".slice (-5);

returns "grade".

This post has been edited by Peter_Kahrel: 18 September 2010 - 05:43 AM