Kebab file names

This commit is contained in:
Angelos Chalaris
2023-04-27 21:58:35 +03:00
parent 1d189c709a
commit 61200d90c4
440 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,28 @@
---
title: String starts with substring
shortTitle: Starts with substring
tags: string
cover: boutique-home-office-3
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 />'); // '/>'
```