string.split()

The string.split() function returns comma-separated list of substrings of $string that are separated by $separator. The separators aren’t included in these substrings.

// Sass.
@use 'sass:list';
@use 'sass:string';

// The `string.split()` function.
@function split($string, $separator, $limit: null, $bracketed: false) {
  $result: list.join((), (), comma, $bracketed);
  $index: 0;
  $i: 1;
  @while $index != null {
    $index: string.index($string, $separator);
    @if $index {
      $result: list.append($result, string.slice($string, 1, $index - 1));
      @if $limit and $limit == $i {
        $result: list.append($result, string.slice($string, $index + 1, -1));
        $index: null;
      } @else {
        $string: string.slice($string, $index + 1);
      }
    } @else {
      $result: list.append($result, $string);
    }

    $i: $i + 1;
  }
  @return $result;
}
https://github.com/angular-package/sass/blob/main/string/_string.split.function.scss

Parameters

$string

The string that is split by $separator.

$separator

The separator that splits the $string into the returned list.

$limit: null

Limit split of $string.

$bracketed

Returns bracketed list.

Return

The return value is a list separated by $separator limited by $limit times, and/or is $bracketed.

Examples

Last updated

Was this helpful?