AppDividend
Latest Code Tutorials

Javascript String Slice Example | String.prototype.slice()

0

Javascript String slice() method extracts a section of the string and returns it as the new string without modifying an original string. We can use the startIndex and endIndex parameters to specify the part of the string you want to extract. The first character has position 0; the second has position 1, and so on. You can use the negative number to select from an end of the string.

Javascript String Slice

The changes to the text in one string do not affect the other string. 

The syntax for the string slice method is the following.

string.slice(startIndex, endIndex)

The startIndex argument is required. It is the position where to begin the extraction.  The first character is at position 0. We can use the negative values to specify the position from the end of the string.

The endIndex argument is optional — the position (up to, but not including) where to end the extraction. If we omit the endIndex parameter then the slice() function selects all characters from the start position to the end of the string. Use the negative numbers to select from the end of the string.

The Javascript string slice() method extracts up to but not including the endIndex.

That means string.slice(1, 6) extracts the second character through the sixth character (characters indexed 1, 2, 3, 4, 5).

// app.js

let str = 'Obama was US president';
let result  = str.slice(2, 9);
console.log(result);

Javascript String Slice Example | String.prototype.slice() Tutorial

Javascript slice() method with negative indexes

The following example uses the Javascript slice() method with negative indexes.

// app.js

let str1 = 'Trump is current US president';
let r1 = str1.slice(-6, -1);
console.log(r1);

String.prototype.slice() Tutorial

Javascript slice function passing one parameter index

If we pass only startIndex, then it will extract the part of the string from the startIndex to the end of the string.

// app.js

let str1 = 'Trump is current US president';
let r1 = str1.slice(1);
console.log(r1);

Save the file and run the javascript file.

Javascript String Slice Example

Javascript Extract Whole String

See the following code.

// app.js

let str2 = "Hakuna Matata";
let res2 = str2.slice(0);
console.log(res2);

Extract the whole string

How To Extract the Last character In Javascript

// app.js

let str3 = "Hakuna Matata";
let res3 = str3.slice(-1);

console.log(res3);

Extract only the last character

The negative index starts from the end of the string. So -1 means the last character of the string.

How To Extract First Character In Javascript

See the following code.

let str = "Millie Bobby Brown";
let res = str.slice(0, 1);
console.log(res);

 See the output.

➜  es git:(master) ✗ node app
M
➜  es git:(master) ✗

We have passed index parameters 0 and 1, which means the first character of the string and remember the string character index starts from 0.

String.slice() vs String.substring() vs String.substr()

Javascript slice(startIndex, endIndex)

The startIndex argument is required. It is the position where to begin the extraction.  The first character is at position 0. Use the negative values to specify the position from the end of the string.

The endIndex argument is optional — the position (up to, but not including) where to end the extraction. If you omitted, slice() selects all characters from the start position to the end of the string. Use the negative integers to select from the end of the string.

Javascript substring(from, to)

Javascript String substring() function takes two arguments.

Argument 1: The from parameter Required. The position where to start the extraction. The first character is at index 0.

Argument 2: The to parameter is Optional. The position (up to, but not including) where to end the extraction. If you omit the to parameter then it extracts the rest of the string.

Javascript substr(start, length)

See the following syntax.

string.substr(start, length);

Javascript substr() method can take two arguments:

Argument 1: The start parameter is required. It is the position where to start the extraction. The first character is at index 0. If you want to extract characters from an end of the string, use a negative start number.

Argument 2: It is the length parameter, and it is optional. The number of characters to extract. If you omit the length parameter then, it extracts the rest of the string.

Finally, the Javascript String Slice Example | String.prototype.slice() article is over.

Related Posts

Javascript String indexOf()

Javascript String concat()

Javascript String split()

Javascript String substring()

Javascript String startswith()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.