Sass string.slice() Function

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

What You’ll Learn

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

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".

📝 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

ParameterTypeRequiredDescription
$stringStringYesThe string to slice.
$start-atNumberYesInclusive start index (1-based or negative).
$end-atNumberNoInclusive end index. Default -1 (last character).

Return value

TypeResult
StringThe inclusive substring from $start-at through $end-at

📦 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);

🎨 Official Patterns

CallResult
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.

⚖️ Inclusive Indexes (Not Like JS)

TopicSass string.sliceJavaScript slice
Start numbering1-based0-based
End indexInclusiveExclusive
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.

🛠 Compatibility

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

Implementationslice
Dart SassYes — prefer string.slice (module since 1.23.0)
LibSassLegacy str-slice may exist; no @use "sass:string"
Ruby SassLegacy str-slice may exist; no @use "sass:string"

Prefer current Dart Sass so the module API matches the official docs.

⚡ Quick Reference

GoalCode
Import string@use "sass:string";
From index to endstring.slice($s, 11)
Fixed rangestring.slice($s, 1, 3)
End from the tailstring.slice($s, 1, -6)
Before a markerstring.slice($s, 1, string.index($s, " ") - 1)
Truncatestring.slice($s, 1, $max) after a length check

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.

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

.probe {
  --from: #{meta.inspect(string.slice("Helvetica Neue", 11))};
  --range: #{meta.inspect(string.slice("Helvetica Neue", 1, 3))};
  --neg: #{meta.inspect(string.slice("Helvetica Neue", 1, -6))};
}

How It Works

Matches the official docs. Omitting $end-at keeps through the last character; -6 stops six characters from the end.

Example 2 — Trim a Font Family Name

Keep the first 14 characters of a longer family string.

styles.scss
@use "sass:string";

.heading {
  font-family: string.slice("Helvetica Neue Bold", 1, 14), sans-serif;
}

How It Works

Indexes 1–14 are exactly "Helvetica Neue". The quoted result is valid CSS for font-family.

📈 Practical Patterns

First-word helpers, separator splits, and truncate.

Example 3 — First Word Helper

Find the space with string.index, then slice before it.

styles.scss
@use "sass:string";

@function first-word($text) {
  $space: string.index($text, " ");
  @if $space == null {
    @return $text;
  }
  @return string.slice($text, 1, $space - 1);
}

.badge {
  content: first-word("Segoe UI");
}

How It Works

The space sits at index 6, so $space - 1 ends on the last letter of Segoe. Guard null when there is no space.

Example 4 — Split on the First Separator

string.index finds the first .; slice before and after it.

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

$file: "logo.dark.svg";
$dot: string.index($file, ".");

.ext {
  --name: #{meta.inspect(string.slice($file, 1, $dot - 1))};
  --ext: #{meta.inspect(string.slice($file, $dot + 1))};
}

How It Works

Index finds the first dot only, so the remainder is "dark.svg"—not just svg. For last-segment splits, keep slicing or use string.split when available.

Example 5 — Truncate Helper

Keep the first n characters after a length check.

styles.scss
@use "sass:string";

@function truncate($text, $max) {
  @if string.length($text) <= $max {
    @return $text;
  }
  @return string.slice($text, 1, $max) + "...";
}

.chip {
  content: truncate("VeryLongLabel", 8);
}

How It Works

Length decides whether to return the full string. string.slice($text, 1, 8) keeps the first eight characters, then appends "...".

🚀 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".

⚠️ 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 onlystring.index + slice splits on the first separator.
  • Expecting mutation — the original string variable stays unchanged.

💡 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

Key Takeaways

Knowledge Unlocked

Five things to remember about string.slice()

Extract an inclusive substring with 1-based or negative indexes.

5
Core concepts
📦 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.

Conclusion

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.

Continue with string.split() or the Sass introduction.

Next: split strings

Learn string.split() to cut a string into a bracketed list.

string.split() →

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