substring-methods
v1.8.3
Published
A collection of useful methods related to substrings, on top of the default ones offered by vanilla JavaScript.
Downloads
24
Maintainers
Readme
Substring Methods
A collection of methods related to substrings in JavaScript
Usage
npm install substring-methods
In your code
const { firstLetter } = require('substring-methods');
console.log(firstLetter("Test string"));
Methods
firstLetter
Get the first letter of a string
const s = firstLetter("Hello");
// returns "H"
lastLetter
Get the last letter of a string
const s = lastLetter("Hello");
// returns "o"
randomLetter
Get a random letter of a string
const s = randomLetter("Hello");
numberOfOccurrences
Count the number of times a substring occurs inside a string
const s = numberOfOccurrences("The heater he ordered for them was hefty", "he");
// returns 5
allPossibleSubstrings
Gets all possible contiguous substrings inside a string
const s = allPossibleSubstrings("cat");
// returns [ 'c', 'a', 't', 'ca', 'at', 'cat' ]
allSubstringsOfLength
Gets all possible contiguous substrings of a given length inside a string
const s = allSubstringsOfLength("word", 2);
// returns [ 'wo', 'or', 'rd' ]