Sass string.slice()

Beginner
8 min read
Updated: Nov 2026
5 examples
Dart Sass 1.23+

Definition and Usage

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.

Syntax

SCSS
@use "sass:string";

string.slice($string, $start-at, $end-at: -1) //=> string

string.slice("Helvetica Neue", 11); // "Neue"
string.slice("Helvetica Neue", 1, 3); // "Hel"
string.slice("Helvetica Neue", 1, -6); // "Helvetica"

// Global alias (legacy): str-slice($string, $start-at, $end-at: -1)
// Prefer string.slice after @use "sass:string"

Pattern: pick start → pick end (or keep default) → get an inclusive substring.

Indexes

How start and end indexes are read:

IndexBehavior
Positive (e.g. 1, 11)Count from the start; 1 is the first character
Negative (e.g. -1, -6)Count from the end; -1 is the last character
Default $end-at-1 — slice through the last character
Both endsInclusive (unlike JS half-open slice)

SCSS Examples

Examples use @use "sass:string". Open View Compiled CSS for verified Dart Sass output (matches official Sass docs samples where shown).

Example 1 — Official Slice Samples

Start-only, inclusive range, and a negative end index.

SCSS
@use "sass:meta";
@use "sass:string";

.probe {
  --a: #{meta.inspect(string.slice("Helvetica Neue", 11))};
  --b: #{meta.inspect(string.slice("Helvetica Neue", 1, 3))};
  --c: #{meta.inspect(string.slice("Helvetica Neue", 1, -6))};
}

How It Works

  1. Start 11 with default end yields "Neue".
  2. 1..3 inclusive yields "Hel".
  3. 1..-6 cuts before the last word.
  4. Matches the official Sass docs samples.

Example 2 — Slice to the End

Omit $end-at to take everything from a start index onward.

SCSS
@use "sass:string";

.tail {
  content: string.slice("Helvetica Neue", 11);
}

How It Works

  1. Default end is -1.
  2. Start at 11 lands on N.
  3. Result is the trailing word.
  4. Handy after finding a delimiter index.

Example 3 — Negative End Index

Count backward from the end to drop a trailing segment.

SCSS
@use "sass:string";

.head {
  content: string.slice("Helvetica Neue", 1, -6);
}

How It Works

  1. -6 is six characters from the end.
  2. Inclusive end keeps the last letter of Helvetica.
  3. Space + Neue are dropped.
  4. Matches the official negative-end sample.

Example 4 — Slice with string.index

Find a delimiter, then cut the left and right parts.

SCSS
@use "sass:meta";
@use "sass:string";

$name: "Helvetica Neue";
$at: string.index($name, " ");
$family: string.slice($name, 1, $at - 1);
$weight: string.slice($name, $at + 1);

.split {
  --family: #{meta.inspect($family)};
  --weight: #{meta.inspect($weight)};
}

How It Works

  1. index finds the space.
  2. Left slice ends just before the space.
  3. Right slice starts just after it.
  4. Pairing index + slice is a common parse pattern.

Example 5 — Truncate with Length

Cap a string at $max characters and append "...".

SCSS
@use "sass:string";

@mixin truncate($text, $max) {
  @if string.length($text) > $max {
    content: string.slice($text, 1, $max) + "...";
  } @else {
    content: $text;
  }
}

.card {
  @include truncate("Helvetica Neue", 9);
}

How It Works

  1. length decides whether to cut.
  2. slice 1..$max keeps the prefix.
  3. "..." marks truncation (ASCII-safe).
  4. @if avoids the deprecated global if().

Edge Cases & Pitfalls

Most slice bugs are off-by-one or JS-style half-open ends.

inclusive

Both ends are inclusive

1, 3 keeps three characters—not a half-open JS range.

1-based

First character is 1

Same indexing as string.index and string.insert.

null

Guard missing index results

If string.index returns null, do not feed it into slice—branch with @if first.

quotes

Quote state is preserved

Slicing a quoted string keeps a quoted result; use meta.inspect when debugging.

Sass Support

Prefer the module form with current Dart Sass.

APIStatus
string.slice()Dart Sass 1.23+ via @use "sass:string"
Return typeString (same quote state as input)
Indexes1-based; both ends inclusive; default end -1
Legacy globalstr-slice($string, $start-at, $end-at: -1)

Compile with the Dart Sass CLI.

Key Takeaways

  • Inclusive 1-based slice from start through end.
  • 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).

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.

Next: Sass string.split()

Split a string on a separator 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.

8 people found this page helpful