Home Sass String string.split() Sass string.split() Definition
Definition and Usage string.split($string, $separator, $limit: null) returns a bracketed, comma-separated list of substrings of $string split on $separator. Separators are not included. Load with @use "sass:string".
Official sample idea. string.split("Segoe UI Emoji", " ") becomes ["Segoe", "UI", "Emoji"].
Optional limit. With $limit: 1 or higher, Sass splits on at most that many separators; the last item keeps the remainder.
Newer API. string.split needs Dart Sass 1.57+ —newer than most other sass:string helpers (1.23+).
Syntax
Syntax @use "sass:string";
string.split($string, $separator, $limit: null) //=> list
string.split("Segoe UI Emoji", " ");
// ["Segoe", "UI", "Emoji"]
string.split("Segoe UI Emoji", " ", $limit: 1);
// ["Segoe", "UI Emoji"]
// No legacy global name — module API only
// Prefer @use "sass:string" (Dart Sass 1.57+) Pattern: pick a separator → get a list of parts → walk with @each or list.nth.
Code
SCSS Examples Examples use @use "sass:string". Open View Compiled CSS for verified Dart Sass output (matches official Sass docs samples where shown).
1. Official 2. Limit 3. list.nth 4. @each 5. Classes Example 1 — Official Split Samples Full split vs a single-separator limit.
@use "sass:meta";
@use "sass:string";
.probe {
--all: #{meta.inspect(string.split("Segoe UI Emoji", " "))};
--limit: #{meta.inspect(string.split("Segoe UI Emoji", " ", $limit: 1))};
} .probe {
--all: ["Segoe", "UI", "Emoji"];
--limit: ["Segoe", "UI Emoji"];
} How It Works Space separator yields three parts.$limit: 1 stops after one split.Last item keeps "UI Emoji".Matches the official Sass docs samples.Example 2 — Multi-Character Separator + Limit Split on "--" fully, then once with a limit.
@use "sass:meta";
@use "sass:string";
$full: string.split("a--b--c", "--");
$once: string.split("a--b--c", "--", $limit: 1);
.compare {
--full: #{meta.inspect($full)};
--once: #{meta.inspect($once)};
} .compare {
--full: ["a", "b", "c"];
--once: ["a", "b--c"];
} How It Works Multi-char separator works like a single char.Full split drops every --.Limit 1 leaves b--c intact.Useful for BEM-like token parsing.Example 3 — Pick a Part with list.nth Split a class-like string, then read one item and the count.
@use "sass:list";
@use "sass:meta";
@use "sass:string";
$parts: string.split("btn primary large", " ");
$mod: list.nth($parts, 2);
.btn {
--mod: #{meta.inspect($mod)};
--count: #{list.length($parts)};
} .btn {
--mod: "primary";
--count: 3;
} How It Works split builds a three-item list.list.nth(..., 2) picks the modifier.list.length reports how many parts.Bridge from string APIs to list APIs.Example 4 — Walk Parts with @each Emit a custom property for each split token.
@use "sass:meta";
@use "sass:string";
$parts: string.split("Segoe UI Emoji", " ");
.loop {
@each $part in $parts {
--p-#{$part}: #{meta.inspect($part)};
}
} .loop {
--p-Segoe: "Segoe";
--p-UI: "UI";
--p-Emoji: "Emoji";
} How It Works split returns a list.@each walks every part.Interpolation builds property names.inspect keeps quote state visible.Example 5 — Generate Classes from Tokens Turn a space-separated token string into utility rules.
@use "sass:string";
$tokens: string.split("primary accent muted", " ");
@each $token in $tokens {
.chip-#{$token} {
content: $token;
}
} .chip-primary {
content: "primary";
}
.chip-accent {
content: "accent";
}
.chip-muted {
content: "muted";
} How It Works One string holds design tokens.split turns it into a list.@each emits a rule per token.Scales config written as plain text. Edge Cases & Pitfalls Most split bugs are wrong limits or treating the result like a string.
list Result is a list, not a string Use list.nth , list.length , or @each—not string helpers on the whole result.
limit Limit counts separators, not items $limit: 1 means one split → up to two parts; leftover separators stay in the last item.
brackets Bracketed comma list meta.inspect shows ["a", "b"]—that bracketed form is intentional.
version Needs Dart Sass 1.57+ Older toolchains may miss string.split even if other sass:string helpers work.
Toolchain
Sass Support Module-only API on current Dart Sass.
API Status string.split()Dart Sass 1.57+ via @use "sass:string" Return type Bracketed, comma-separated list Separators Not included in the substrings Legacy global None — no str-split()
Compile with the Dart Sass CLI .
Remember
Key Takeaways Returns a bracketed list of substrings.
$limit caps splits — last item keeps the remainder.
Pair with list APIs — nth, length, @each.
Dart Sass 1.57+ — no legacy global name.
One line: to turn a string into a list of parts, use string.split($string, $separator).
Practice
Compile Notes Practical checklist:
1. @use "sass:string" (Dart Sass 1.57+)
2. Pass $string and $separator
3. Expect a bracketed comma-separated list
4. Use $limit when you need a head / remainder split
5. Walk results with @each or list.nth / list.length Frequently Asked Questions What does Sass string.split() do? string.split($string, $separator, $limit: null) returns a bracketed, comma-separated list of substrings split on $separator. Example: string.split("Segoe UI Emoji", " ") is ["Segoe", "UI", "Emoji"].
Are separators included in the result? No. The separators are not included in the substrings.
What does $limit do? If $limit is a number 1 or higher, Sass splits on at most that many separators and returns at most $limit + 1 strings. The last substring keeps the rest of the string, including remaining separators. Example: string.split("Segoe UI Emoji", " ", $limit: 1) is ["Segoe", "UI Emoji"].
What type does split return? A bracketed, comma-separated list. Use list.nth, list.length, and @each to work with the pieces.
Is there a global str-split()? No. Official docs document only string.split on the sass:string module.
Which compilers support string.split? Dart Sass 1.57+. LibSass and Ruby Sass do not support this function.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion