Sass string.split() Function

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
sass:string · Dart Sass 1.57+

What You’ll Learn

string.split() belongs to the built-in sass:string module. It turns one string into a list of pieces using a separator. This page covers the optional $limit, pairing with sass:list, and five compiled examples.

01

Concept

Split into a list

02

Module

@use "sass:string"

03

Returns

Bracketed list

04

Limit

Optional $limit

05

Needs

Dart Sass 1.57+

06

Practice

5 examples

What Is string.split()?

Official docs: returns a bracketed, comma-separated list of substrings of $string that are separated by $separator. The separators are not included in those substrings.

  • You get a list, not a single string—use list.nth or @each.
  • Optional $limit stops after a fixed number of splits.
  • When limited, the last piece keeps whatever is left (including leftover separators).
  • Requires Dart Sass 1.57+ (newer than most other sass:string helpers).
💡
Beginner tip

Picture cutting a sentence at every space. "Segoe UI Emoji" becomes the list ["Segoe", "UI", "Emoji"].

📝 Syntax

styles.scss
@use "sass:string";

// Module API only (Dart Sass 1.57+)
string.split($string, $separator, $limit: null)

Parameters

ParameterTypeRequiredDescription
$stringStringYesThe text to split.
$separatorStringYesWhere to cut (space, /, ,, --, …).
$limitNumber or nullNoMax number of separator cuts. Default null = split everywhere.

Return value

TypeResult
ListBracketed, comma-separated list of substrings (separators removed)

📦 Loading sass:string

styles.scss
// Recommended — namespaced (Dart Sass 1.57+)
@use "sass:string";
$parts: string.split("Segoe UI Emoji", " "); // ["Segoe", "UI", "Emoji"]

// Optional — bring members into scope
@use "sass:string" as *;
$parts: split("Segoe UI Emoji", " ");
⚠️
No legacy global

Unlike str-length / str-slice, official docs do not provide a global str-split(). Use the module API on Dart Sass 1.57+.

🎨 Official Patterns

CallResult
split("Segoe UI Emoji", " ")["Segoe", "UI", "Emoji"]
split("Segoe UI Emoji", " ", $limit: 1)["Segoe", "UI Emoji"]

With $limit: 1, Sass cuts once. The second item keeps the remaining space and text.

⚖️ Understanding $limit

$limitWhat happensMax items
null (default)Split on every separatorUnlimited
1 or higherSplit on at most that many separators$limit + 1
💡
Remember the leftover

After the allowed cuts, the last substring contains the rest of the string, including any remaining separators. That is why "UI Emoji" still has a space.

🛠 Compatibility

This is about Sass compilers, not browsers. Splitting happens at compile time.

Implementationsplit
Dart SassYes — string.split since 1.57.0
LibSassNo
Ruby SassNo

Prefer current Dart Sass 1.57+. Other string helpers often need only 1.23+; split is newer.

⚡ Quick Reference

GoalCode
Import string@use "sass:string";
Split on spacesstring.split($s, " ")
Split oncestring.split($s, " ", $limit: 1)
Get first piecelist.nth(string.split($s, "/"), 1)
Get last piecelist.nth(string.split($s, "/"), -1)
Loop pieces@each $part in string.split($s, ",") { ... }

Examples Gallery

Each example uses string.split at compile time (Dart Sass 1.57+). Open View Compiled CSS for verified output.

📚 Getting Started

Official docs samples, then a font-family stack from parts.

Example 1 — Official Split Samples

Full split vs a one-cut $limit.

styles.scss
@use "sass:meta";
@use "sass:string";

.probe {
  --all: #{meta.inspect(string.split("Segoe UI Emoji", " "))};
  --limited: #{meta.inspect(string.split("Segoe UI Emoji", " ", $limit: 1))};
}

How It Works

Matches the official docs. meta.inspect shows the bracketed list shape clearly in custom properties.

Example 2 — Font Stack From Words

Split a family string and rebuild font-family with list.nth.

styles.scss
@use "sass:list";
@use "sass:string";

$parts: string.split("Segoe UI Emoji", " ");

.font {
  font-family: list.nth($parts, 1), list.nth($parts, 2), list.nth($parts, 3), sans-serif;
}

How It Works

Split returns a list; list.nth pulls each word into the CSS value list. A fallback sans-serif stays at the end.

📈 Practical Patterns

Paths, limited BEM cuts, and looping CSV tokens.

Example 3 — Path Segments

Split on /, then read depth and the leaf name.

styles.scss
@use "sass:list";
@use "sass:string";

$path: "themes/dark/card";
$segments: string.split($path, "/");

.card {
  --depth: #{list.length($segments)};
  --leaf: #{list.nth($segments, -1)};
}

How It Works

Three segments mean depth 3. Negative list indexes read from the end, so -1 is the leaf card.

Example 4 — Limit a BEM-Style Cut

Split once on -- to separate the block from the rest.

styles.scss
@use "sass:list";
@use "sass:string";

$class: "btn--primary--lg";
$parts: string.split($class, "--", $limit: 1);

.mod {
  --block: #{list.nth($parts, 1)};
  --rest: #{list.nth($parts, 2)};
}

How It Works

One cut yields two items. The leftover -- stays inside primary--lg because only the first separator was used.

Example 5 — Loop CSV Tokens With @each

Split a comma list and emit one rule per color.

styles.scss
@use "sass:string";

$csv: "red,green,blue";
$colors: string.split($csv, ",");

@each $color in $colors {
  .swatch-#{$color} {
    background: #{$color};
  }
}

How It Works

Split turns the CSV into a list. @each walks every item and interpolates both the class name and the background value.

🚀 Real-World Use Cases

  • Font stacks — break a family string into individual names.
  • Path tooling — read folders and leaf names from / segments.
  • BEM helpers — cut once on -- to separate block vs modifiers.
  • Token loops — turn CSV-like strings into @each generators.
  • Config strings — parse simple space- or comma-delimited options at compile time.

🧠 How Compilation Works

1

Pass string and separator

Call string.split($string, $separator, $limit).

Source
2

Sass cuts into a list

Removes separators; applies $limit when set.

Compile
3

Use list helpers

Read with list.nth, count with list.length, or loop with @each.

Emit
4

CSS ships

Browsers only see finished values like "Segoe" or red.

⚠️ Common Pitfalls

  • Expecting a string — the return value is a list.
  • Forgetting leftover separators — with $limit, the last item may still contain separators.
  • Old compilers — needs Dart Sass 1.57+; LibSass/Ruby Sass cannot run it.
  • Looking for str-split — there is no official legacy global name.
  • Confusing with string.slice — slice returns one substring; split returns many pieces.

💡 Best Practices

✅ Do

  • Use @use "sass:string" and Dart Sass 1.57+
  • Pair with @use "sass:list" for nth / length
  • Use $limit when you only want the first cut
  • Inspect lists with meta.inspect while learning
  • Prefer clear separators (" ", "/", ",")

❌ Don’t

  • Treat the result like a single string
  • Forget that limited splits keep leftover separators
  • Expect LibSass or Ruby Sass support
  • Search for a global str-split()
  • Expect browsers to evaluate the call

Key Takeaways

Knowledge Unlocked

Five things to remember about string.split()

Cut a string into a bracketed list of pieces at compile time.

5
Core concepts
📦 02

Module

sass:string

@use
🔗 03

Result

bracketed list

Output
📚 04

Limit

max cuts + leftover

Rule
05

Pair

list.nth / @each

Pattern

❓ Frequently Asked Questions

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"].
No. Official docs: the separators are not included in the substrings.
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"].
A list (bracketed and comma-separated). Use list.nth, list.length, and @each to work with the pieces.
No. Official docs document only string.split on the sass:string module. Prefer @use "sass:string" and Dart Sass 1.57+.
Dart Sass 1.57+. LibSass and Ruby Sass do not support this function.
Did you know?

Official Sass docs show $limit: 1 turning "Segoe UI Emoji" into ["Segoe", "UI Emoji"]—proof that the leftover piece keeps remaining separators.

Conclusion

string.split() turns a string into a bracketed list of pieces. Separators are removed, optional $limit caps how many cuts happen, and the last leftover piece can still contain separators. Use list.nth and @each to work with the result on Dart Sass 1.57+.

Continue with string.to-lower-case() or the Sass introduction.

Next: lowercase strings

Learn string.to-lower-case() to convert ASCII letters at compile time.

string.to-lower-case() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful