Skip to content

Commit

Permalink
Add substring snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
Chalarangelo committed Jul 16, 2022
1 parent 82a94e1 commit 674bd91
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
29 changes: 29 additions & 0 deletions snippets/endsWithSubstring.md
Original file line number Diff line number Diff line change
@@ -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 changes: 25 additions & 0 deletions snippets/leftSubstrGenerator.md
Original file line number Diff line number Diff line change
@@ -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 changes: 25 additions & 0 deletions snippets/rightSubstrGenerator.md
Original file line number Diff line number Diff line change
@@ -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 changes: 29 additions & 0 deletions snippets/startsWithSubstring.md
Original file line number Diff line number Diff line change
@@ -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 />'); // '/>'
```

0 comments on commit 674bd91

Please sign in to comment.