Created
June 26, 2022 15:38
-
-
Save kadimi/32bb8f4cc56224343992806fd0898322 to your computer and use it in GitHub Desktop.
Remove ending substring from a string
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
/** | |
* `removeEndingSubString` removes a substring from the end of a string. | |
* | |
* It will only remove the exact substring. So if the string doesn't end | |
* with the substring, nothing happens and the original string is returned | |
* as is. | |
* | |
* @link https://kadimi.com/ | https://gist.github.com/kadimi/32bb8f4cc56224343992806fd0898322 | |
* @author Nabil Kadimi | |
* | |
* @param {string} original The origin string. | |
* @param {string} substringToRemove the string to remove. | |
* @returns The new string. | |
*/ | |
function removeEndingSubString(original, substringToRemove) { | |
if (original.slice(0 - substringToRemove.length) === substringToRemove) { | |
return original.substring(0, original.length - substringToRemove.length); | |
} | |
return original; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment