String Extraction
Description :
In Javascript, ther are different ways of extracting a string from another string.
Some of these are :
slice(start, end)
The slice() method essentially extracts the string between start(inclusive)
and end(exclusive) and returns it in a new string
If a parameter is negative, it is counted from the end of the string.
If the second parameter is left out, the end is taken to be the end of the string.
Syntax :
let sub_string = original_string.slice(start_pos, end_pos);
Examples :
let str = "Apple, Banana, Kiwi";
let res = str.slice(7,13);
res = ??
let str = "Apple, Banana, Kiwi";
let res = str.slice(-12,-6);
res = ??
let str = "Apple, Banana, Kiwi";
let res = str.slice(7);
res = ??
substring(start, end)
The substring method is similar to the slice method except that it doesn't accept negative indices.
Examples :
let str = "Apple, Banana, Kiwi";
let res = str.slice(7,13);
res = ??
substr(start, length)
The substring method is similar to the slice method except that the second parameter is the length of the string that you want to extract.
Examples :
let str = "Apple, Banana, Kiwi";
let res = str.substr(7,6);
res = ??