Skip to content

Instantly share code, notes, and snippets.

@kadimi
Created June 26, 2022 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kadimi/32bb8f4cc56224343992806fd0898322 to your computer and use it in GitHub Desktop.
Save kadimi/32bb8f4cc56224343992806fd0898322 to your computer and use it in GitHub Desktop.
Remove ending substring from a string
/**
* `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