Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!
First, let’s review what the `slice()` method does. It returns a portion of a string based on the starting and ending index positions. For example, if we have the string “hello world” and we want to extract the word “world”, we can use the `slice()` method like this:
let str = "hello world";
let sliced = str.slice(6, 11);
console.log(sliced); // "world"
Here’s an example:
sliced=$(echo $str | cut -c7-11)
echo $sliced # "world"
In this example, we first set the value of the `str` variable to “hello world”. We then use the `echo` command to output the value of `str` and pipe it to the `cut` command. The `-c` option tells `cut` to extract characters, and the `7-11` argument specifies the starting and ending index positions. Finally, we assign the result to the `sliced` variable and output it using the `echo` command.