string.slice() belongs to the built-in sass:string module. It returns an inclusive substring using 1-based (or negative) indexes. This page covers the default end index, pairing with string.index, truncate helpers, and five compiled examples.
01
Concept
Extract a substring
02
Module
@use "sass:string"
03
Returns
New string
04
Indexes
Inclusive + / −
05
Default end
-1
06
Practice
5 examples
Concept
What Is string.slice()?
Official docs: returns the slice of $string starting at index $start-at and ending at index $end-at(both inclusive).
Indexes are 1-based from the start, or negative from the end.
Omit $end-at (or pass -1) to keep everything through the last character.
The original string is not mutated—you get a new string.
Pair with string.index and string.length for dynamic ranges.
💡
Beginner tip
Think of highlighting letters on a ruler. In "Helvetica Neue", slicing from index 11 to the end highlights "Neue".
Foundation
📝 Syntax
styles.scss
@use "sass:string";
// Recommended
string.slice($string, $start-at, $end-at: -1)
// Legacy global name
str-slice($string, $start-at, $end-at: -1)
Parameters
Parameter
Type
Required
Description
$string
String
Yes
The string to slice.
$start-at
Number
Yes
Inclusive start index (1-based or negative).
$end-at
Number
No
Inclusive end index. Default -1 (last character).
Return value
Type
Result
String
The inclusive substring from $start-at through $end-at
Modules
📦 Loading sass:string
styles.scss
// Recommended — namespaced (Dart Sass 1.23+)
@use "sass:string";
$part: string.slice("Helvetica Neue", 11); // "Neue"
// Optional — bring members into scope
@use "sass:string" as *;
$part: slice("Helvetica Neue", 11);
// Legacy global
$part: str-slice("Helvetica Neue", 11);
Details
🎨 Official Patterns
Call
Result
slice("Helvetica Neue", 11)
"Neue" (to end)
slice("Helvetica Neue", 1, 3)
"Hel"
slice("Helvetica Neue", 1, -6)
"Helvetica"
These are the official Sass documentation samples. The end index is inclusive, and -6 counts from the end of the string.
Compare
⚖️ Inclusive Indexes (Not Like JS)
Topic
Sass string.slice
JavaScript slice
Start numbering
1-based
0-based
End index
Inclusive
Exclusive
Default end
-1 (last char)
End of string
⚠️
Watch the end index
string.slice("Helvetica Neue", 1, 3) keeps indexes 1, 2, and 3 → "Hel". In JavaScript, "Helvetica Neue".slice(0, 3) also gives "Hel", but for different indexing rules.
Support
🛠 Compatibility
This is about Sass compilers, not browsers. Slicing happens at compile time.
Implementation
slice
Dart Sass
Yes — prefer string.slice (module since 1.23.0)
LibSass
Legacy str-slice may exist; no @use "sass:string"
Ruby Sass
Legacy str-slice may exist; no @use "sass:string"
Prefer current Dart Sass so the module API matches the official docs.
Cheat Sheet
⚡ Quick Reference
Goal
Code
Import string
@use "sass:string";
From index to end
string.slice($s, 11)
Fixed range
string.slice($s, 1, 3)
End from the tail
string.slice($s, 1, -6)
Before a marker
string.slice($s, 1, string.index($s, " ") - 1)
Truncate
string.slice($s, 1, $max) after a length check
Hands-On
Examples Gallery
Each example uses string.slice at compile time (Dart Sass 1.23+). Open View Compiled CSS for verified output.
📚 Getting Started
Official docs samples, then a font-family trim.
Example 1 — Official Slice Samples
To the end, a short range, and a negative end index.
Length decides whether to return the full string. string.slice($text, 1, 8) keeps the first eight characters, then appends "...".
Applications
🚀 Real-World Use Cases
Prefixes / suffixes — keep a known head or tail of a token.
First-word labels — pair with string.index for spaces.
Path / filename parts — slice around the first separator.
Truncate chips — combine with string.length for max-width text.
Font tooling — trim weight words off a family string before CSS emit.
🧠 How Compilation Works
1
Pass string and indexes
Call string.slice($string, $start-at, $end-at).
Source
2
Sass resolves the range
Applies 1-based / negative indexes; both ends are inclusive.
Compile
3
Use the substring
Assign to CSS properties or feed another string helper.
Emit
4
✓
CSS ships
Browsers only see finished text like "Neue" or "Helvetica".
Watch Out
⚠️ Common Pitfalls
0-based habits — Sass string indexes start at 1.
Exclusive-end habits — Sass end indexes are inclusive.
Forgetting default -1 — omitting the end means “through the last character.”
First match only — string.index + slice splits on the first separator.
Expecting mutation — the original string variable stays unchanged.
Pro Tips
💡 Best Practices
✅ Do
Use @use "sass:string" and string.slice()
Remember inclusive 1-based indexes
Pair with string.index for dynamic cuts
Guard null when a marker might be missing
Prefer Dart Sass 1.23+
❌ Don’t
Assume JavaScript 0-based / exclusive-end rules
Forget that default $end-at is -1
Expect the original string variable to change
Expect browsers to evaluate the call
Rely on LibSass for the module API
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about string.slice()
Extract an inclusive substring with 1-based or negative indexes.
5
Core concepts
📝01
API
slice(s, start, end)
Call
📦02
Module
sass:string
@use
🔗03
Result
new substring
Output
📚04
Indexes
inclusive + / −
Rule
✓05
Pair
index then slice
Pattern
❓ 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. Official docs: both $start-at and $end-at are inclusive. string.slice("Helvetica Neue", 1, 3) is "Hel".
The default is -1, which means 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.
Did you know?
Official Sass docs show string.slice("Helvetica Neue", 1, -6) returning "Helvetica"—negative end indexes count from the tail while staying inclusive.
string.slice() returns an inclusive substring using 1-based or negative indexes. The default end is -1 (through the last character). Pair it with string.index and string.length for dynamic cuts and truncate helpers.