Sass string.length()

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

Definition and Usage

string.length($string) returns the number of characters in $string. An empty string is 0. Load with @use "sass:string".

Official sample idea.

length("Helvetica Neue") → 14; length(bold) → 4; length("") → 0.

Quotes are not characters.

Quoted and unquoted forms of the same text have the same length. Spaces do count.

Prefer the namespaced name.

Use string.length, not bare length(), so you do not clash with list helpers. Pair with string.slice() for truncating.

Syntax

SCSS
@use "sass:string";

string.length($string) //=> number

string.length("Helvetica Neue"); // 14
string.length(bold); // 4
string.length(""); // 0

// Guards:
@if string.length($s) > 10 { ... }
@if string.length($s) == 0 { ... }

// Global alias (legacy): str-length($string)
// Prefer string.length after @use "sass:string"

Pattern: pass a string → get a character count → branch, size, or feed into slice/insert.

What Counts

Quick facts for everyday use:

InputLength
"Helvetica Neue"14 (space included)
bold (unquoted)4
""0
"Roboto" vs RobotoBoth 6

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 Length Samples

Quoted name, unquoted weight, and empty string.

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

.probe {
  --quoted: #{meta.inspect(string.length("Helvetica Neue"))};
  --unquoted: #{meta.inspect(string.length(bold))};
  --empty: #{meta.inspect(string.length(""))};
}

How It Works

  1. Helvetica Neue includes the space → 14.
  2. bold unquoted still has four characters.
  3. Empty string is 0.
  4. Matches the official Sass docs samples.

Example 2 — Quotes Do Not Change Length

Quoted and unquoted Roboto both count six characters; spaces still count.

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

.compare {
  --a: #{meta.inspect(string.length("Roboto"))};
  --b: #{meta.inspect(string.length(Roboto))};
  --space: #{meta.inspect(string.length("a b"))};
}

How It Works

  1. Quotes are storage, not content.
  2. Both forms of Roboto are length 6.
  3. Space in "a b" counts.
  4. Length is 3 for that sample.

Example 3 — Max-Length Guard

Branch when a label is longer than a limit.

SCSS
@use "sass:string";

$name: "Helvetica Neue";
$max: 10;

.card {
  @if string.length($name) > $max {
    content: "too-long";
  } @else {
    content: $name;
  }
}

How It Works

  1. 14 > 10, so the @if branch wins.
  2. Compile-time decision only.
  3. Browsers never see string.length.
  4. Useful for design-system label checks.

Example 4 — Size With ch Units

Use character count to set a minimum inline size.

SCSS
@use "sass:string";

$label: "Save";

.btn::after {
  content: $label;
  min-inline-size: string.length($label) * 1ch;
}

How It Works

  1. Save has four characters.
  2. Sass multiplies by 1ch.
  3. Emits 4ch in the finished CSS.
  4. Handy for chips and icon buttons.

Example 5 — Truncate Helper

Keep the first n characters and append dots when needed.

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

  1. length decides whether to truncate.
  2. slice keeps the first 8 characters.
  3. "..." marks the cut.
  4. Indexes for slice are 1-based too.

Edge Cases & Pitfalls

Most length bugs are counting quotes or using bare length().

quotes

Quotes are not characters

Quoted and unquoted forms of the same text share a length.

space

Spaces do count

"a b" is length 3.

empty

Empty string is 0

Use string.length($s) == 0 for empty checks.

name

Prefer string.length

Avoid bare length() so you do not clash with list helpers.

Sass Support

Prefer the module form with current Dart Sass.

APIStatus
string.length()Dart Sass 1.23+ via @use "sass:string"
Return typeNumber (character count)
Empty string0
Legacy globalstr-length($string)

Compile with the Dart Sass CLI.

Key Takeaways

  • Returns a character count for any Sass string.
  • Empty is 0; spaces count; quotes do not.
  • Pairs with slice for truncate helpers.
  • Prefer @use "sass:string" (Dart Sass 1.23+).

One line: to count characters in a string, use string.length($string).

Compile Notes

Practical checklist:

checklist.txt
1. @use "sass:string"
2. Pass a quoted or unquoted string
3. Expect a character count (empty = 0)
4. Branch with @if on the number
5. Pair with slice / insert for transforms

Frequently Asked Questions

string.length($string) returns the number of characters in $string. Example: string.length("Helvetica Neue") is 14.
Yes. string.length("") is 0.
No. Quotes mark how Sass stores the string; they are not part of the character count. string.length("Roboto") and string.length(Roboto) are both 6.
Yes. Spaces are characters. "a b" has length 3.
Yes. str-length($string) is the legacy global name. Prefer string.length 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.quote()

Turn a value into a quoted Sass string.

string.quote() →

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