Skip to content

Instantly share code, notes, and snippets.

@jakejarvis
Created August 17, 2021 19:33
Show Gist options
  • Save jakejarvis/f48bce449392b5ef56db15f28525b46c to your computer and use it in GitHub Desktop.
Save jakejarvis/f48bce449392b5ef56db15f28525b46c to your computer and use it in GitHub Desktop.
SCSS string split function
// Returns a list of strings from a given string separated by a given
// separator (defaults to comma).
// https://stackoverflow.com/a/65853667/1438024
@function str-split($str, $delimiter: ",") {
// return immediately if this function isn't necessary (aka separator not found)
@if (string.index("#{$str}", "#{$delimiter}") == null) {
@return $str;
}
$str-list: ();
@while string.index("#{$str}", "#{$delimiter}") != null {
@if (string.index("#{$str}", "#{$delimiter}") > 1) {
$str-list: list.append($str-list, string.slice("#{$str}", 1, string.index("#{$str}", "#{$delimiter}") - 1));
}
$str: string.slice("#{$str}", string.index("#{$str}", "#{$delimiter}") + 1, string.length("#{$str}"));
}
@if (string.slice("#{$str}", 1, string.length("#{$str}")) != "") {
$str-list: list.append($str-list, string.slice("#{$str}", 1, string.length("#{$str}")));
}
@return $str-list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment