640 B
640 B
title, tags, cover, author, firstSeen
| title | tags | cover | author | firstSeen |
|---|---|---|---|---|
| Right substring generator | string,generator | blog_images/boutique-home-office-2.jpg | chalarangelo | 2022-07-25T05:00:00-04:00 |
Generates all right substrings of a given string.
- Use
String.prototype.lengthto terminate early if the string is empty. - Use a
for...inloop andString.prototype.slice()toyieldeach substring of the given string, starting at the end.
const rightSubstrGenerator = function* (str) {
if (!str.length) return;
for (let i in str) yield str.slice(-i - 1);
};
[...rightSubstrGenerator('hello')];
// [ 'o', 'lo', 'llo', 'ello', 'hello' ]