Add substring snippets
This commit is contained in:
29
snippets/endsWithSubstring.md
Normal file
29
snippets/endsWithSubstring.md
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
---
|
||||||
|
title: String ends with substring
|
||||||
|
shortTitle: Ends with substring
|
||||||
|
tags: string
|
||||||
|
expertise: beginner
|
||||||
|
cover: blog_images/boutique-home-office-4.jpg
|
||||||
|
author: chalarangelo
|
||||||
|
firstSeen: 2022-08-01T05:00:00-04:00
|
||||||
|
---
|
||||||
|
|
||||||
|
Checks if a given string ends with a substring of another string.
|
||||||
|
|
||||||
|
- Use a `for...in` loop and `String.prototype.slice()` to get each substring of the given `word`, starting at the end.
|
||||||
|
- Use `String.prototype.endsWith()` to check the current substring against the `text`.
|
||||||
|
- Return the matching substring, if found. Otherwise, return `undefined`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const endsWithSubstring = (text, word) => {
|
||||||
|
for (let i in word) {
|
||||||
|
const substr = word.slice(0, i + 1);
|
||||||
|
if (text.endsWith(substr)) return substr;
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
endsWithSubstring('Lorem ipsum dolor sit amet<br /', '<br />'); // '<br /'
|
||||||
|
```
|
||||||
25
snippets/leftSubstrGenerator.md
Normal file
25
snippets/leftSubstrGenerator.md
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
---
|
||||||
|
title: Left substring generator
|
||||||
|
tags: string,generator
|
||||||
|
expertise: intermediate
|
||||||
|
cover: blog_images/boutique-home-office-1.jpg
|
||||||
|
author: chalarangelo
|
||||||
|
firstSeen: 2022-07-24T05:00:00-04:00
|
||||||
|
---
|
||||||
|
|
||||||
|
Generates all left substrings of a given string.
|
||||||
|
|
||||||
|
- Use `String.prototype.length` to terminate early if the string is empty.
|
||||||
|
- Use a `for...in` loop and `String.prototype.slice()` to `yield` each substring of the given string, starting at the beginning.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const leftSubstrGenerator = function* (str) {
|
||||||
|
if (!str.length) return;
|
||||||
|
for (let i in str) yield str.slice(0, i + 1);
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
[...leftSubstrGenerator('hello')];
|
||||||
|
// [ 'h', 'he', 'hel', 'hell', 'hello' ]
|
||||||
|
```
|
||||||
25
snippets/rightSubstrGenerator.md
Normal file
25
snippets/rightSubstrGenerator.md
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
---
|
||||||
|
title: Right substring generator
|
||||||
|
tags: string,generator
|
||||||
|
expertise: intermediate
|
||||||
|
cover: blog_images/boutique-home-office-2.jpg
|
||||||
|
author: chalarangelo
|
||||||
|
firstSeen: 2022-07-25T05:00:00-04:00
|
||||||
|
---
|
||||||
|
|
||||||
|
Generates all right substrings of a given string.
|
||||||
|
|
||||||
|
- Use `String.prototype.length` to terminate early if the string is empty.
|
||||||
|
- Use a `for...in` loop and `String.prototype.slice()` to `yield` each substring of the given string, starting at the end.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const rightSubstrGenerator = function* (str) {
|
||||||
|
if (!str.length) return;
|
||||||
|
for (let i in str) yield str.slice(-i - 1);
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
[...rightSubstrGenerator('hello')];
|
||||||
|
// [ 'o', 'lo', 'llo', 'ello', 'hello' ]
|
||||||
|
```
|
||||||
29
snippets/startsWithSubstring.md
Normal file
29
snippets/startsWithSubstring.md
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
---
|
||||||
|
title: String starts with substring
|
||||||
|
shortTitle: Starts with substring
|
||||||
|
tags: string
|
||||||
|
expertise: beginner
|
||||||
|
cover: blog_images/boutique-home-office-3.jpg
|
||||||
|
author: chalarangelo
|
||||||
|
firstSeen: 2022-07-31T05:00:00-04:00
|
||||||
|
---
|
||||||
|
|
||||||
|
Checks if a given string starts with a substring of another string.
|
||||||
|
|
||||||
|
- Use a `for...in` loop and `String.prototype.slice()` to get each substring of the given `word`, starting at the beginning.
|
||||||
|
- Use `String.prototype.startsWith()` to check the current substring against the `text`.
|
||||||
|
- Return the matching substring, if found. Otherwise, return `undefined`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const startsWithSubstring = (text, word) => {
|
||||||
|
for (let i in word) {
|
||||||
|
const substr = word.slice(-i - 1);
|
||||||
|
if (text.startsWith(substr)) return substr;
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
startsWithSubstring('/>Lorem ipsum dolor sit amet', '<br />'); // '/>'
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user