Skip to content

Instantly share code, notes, and snippets.

@joram77
Last active March 13, 2021 11:32
Show Gist options
  • Save joram77/a563b822b150309fbfdbf23d1dd2f136 to your computer and use it in GitHub Desktop.
Save joram77/a563b822b150309fbfdbf23d1dd2f136 to your computer and use it in GitHub Desktop.
/** Substring of a given string until the first space occuring before the given max length. **/
function substringUntilSpace(start, length, s) {
let trimmedString;
if (s.length > length) {
trimmedString = s.substr(start, length);
if (s.charAt((start + length)) != ' ') {
const lispace = trimmedString.lastIndexOf(" ");
if (lispace != -1) trimmedString = trimmedString.substr(start, lispace);
}
} else trimmedString = s;
return trimmedString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment