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;
}

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

// Use.
@use '@angular-package/sass/string';

// Examples.
@debug string.split('aaa bbb ccc', ' '); // "aaa", "bbb", "ccc"
@debug string.split('aaa-bbb-ccc', '-'); // "aaa", "bbb", "ccc"
@debug string.split('aaa/bbb/ccc', '/'); // "aaa", "bbb", "ccc"
@debug string.split("Segoe UI Emoji", " "); // "Segoe", "UI", "Emoji"
@debug string.split("Segoe UI Emoji", " ", $limit: 1); // "Segoe", "UI Emoji"
@debug string.split("Segoe UI Emoji", " ", $limit: 1, $bracketed: true); // ["Segoe", "UI Emoji"]
@debug string.split("SF Mono Segoe UI Mono Roboto Mono", " ", $limit: 2, $bracketed: true); // ["SF", "Mono", "Segoe UI Mono Roboto Mono"]

Last updated