string.slice($string, $start-at, $end-at: -1) returns the slice of $string from index $start-at through $end-at (both inclusive). Indexes are 1-based. Load with @use "sass:string".
Official sample idea.
string.slice("Helvetica Neue", 11) is "Neue"; string.slice("Helvetica Neue", 1, 3) is "Hel".
Default end.
$end-at defaults to -1 (last character), so omitting it slices from the start index to the end.
Not like JS slice.
JavaScript’s String#slice uses a half-open end. Sass includes both ends—plan indexes accordingly.
Default end is -1 — omit it to take the rest of the string.
Pair with index/length for parsing and truncation.
Prefer @use "sass:string" (Dart Sass 1.23+).
One line: to cut an inclusive substring, use string.slice($string, $start-at, $end-at).
Practice
Compile Notes
Practical checklist:
checklist.txt
1. @use "sass:string"
2. Remember indexes are 1-based and inclusive
3. Omit $end-at to slice through the last character
4. Use negative indexes to count from the end
5. Pair with string.index / string.length for real workflows
Frequently Asked Questions
string.slice($string, $start-at, $end-at: -1) returns the inclusive substring from $start-at through $end-at. Example: string.slice("Helvetica Neue", 11) is "Neue".
Yes. Both $start-at and $end-at are inclusive. string.slice("Helvetica Neue", 1, 3) is "Hel".
The default is -1 (the last character). Omitting $end-at slices from $start-at to the end of the string.
Negative indexes count from the end. Official sample: string.slice("Helvetica Neue", 1, -6) is "Helvetica".
Yes. str-slice($string, $start-at, $end-at: -1) is the legacy global name. Prefer string.slice after @use "sass:string".
Dart Sass 1.23+. LibSass and Ruby Sass do not load sass:string with @use; they may still offer the legacy global name.