forked from Chalarangelo/30-seconds-of-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
82a94e1
commit 674bd91
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 /' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 />'); // '/>' | ||
``` |