Sass string.length() Function

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

What You’ll Learn

string.length() belongs to the built-in sass:string module. It returns how many characters a string has. This page covers empty strings, quoted vs unquoted values, length guards, truncate helpers, and five compiled examples.

01

Concept

Count characters

02

Module

@use "sass:string"

03

Returns

Number

04

Empty

0

05

Quotes

Not counted

06

Practice

5 examples

What Is string.length()?

Official docs: returns the number of characters in $string. Spaces count. An empty string has length 0.

  • Works on quoted and unquoted Sass strings.
  • The surrounding quotes are not characters in the value.
  • Use it before string.slice when you only want to keep the first n characters.
  • Pair with @if for max-length guards on labels and class names.
💡
Beginner tip

Think of counting letters on a name tag. "Helvetica Neue" has 14 characters (including the space). bold has 4.

📝 Syntax

styles.scss
@use "sass:string";

// Recommended
string.length($string)

// Legacy global name
str-length($string)

Parameters

ParameterTypeRequiredDescription
$stringStringYesThe string whose characters you want to count.

Return value

TypeResult
NumberCharacter count (unitless). Empty string → 0

📦 Loading sass:string

styles.scss
// Recommended — namespaced (Dart Sass 1.23+)
@use "sass:string";
$count: string.length("Helvetica Neue"); // 14

// Optional — bring members into scope
@use "sass:string" as *;
$count: length("Helvetica Neue");

// Legacy global
$count: str-length("Helvetica Neue");
⚠️
Name clash tip

With @use "sass:string" as *;, bare length() can collide with list.length if you also import list members. Prefer the namespaced form string.length().

🎨 Official Patterns

CallResult
length("Helvetica Neue")14
length(bold)4
length("")0

These are the official Sass documentation samples. Notice both quoted and unquoted inputs are valid strings.

⚖️ Quotes, Spaces & Empty Strings

ValueLengthWhy
"Roboto"6Quotes are not counted
Roboto (unquoted)6Same characters
"a b"3Space counts as one character
""0Empty string

🛠 Compatibility

This is about Sass compilers, not browsers. Length is computed at compile time.

Implementationlength
Dart SassYes — prefer string.length (module since 1.23.0)
LibSassLegacy str-length may exist; no @use "sass:string"
Ruby SassLegacy str-length 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";
Count charactersstring.length("Helvetica Neue")
Empty checkstring.length($s) == 0
Max-length guard@if string.length($s) > 10 { ... }
Size with chstring.length($label) * 1ch
Truncatestring.slice($s, 1, $max) after a length check

Examples Gallery

Each example uses string.length at compile time (Dart Sass 1.23+). Open View Compiled CSS for verified output.

📚 Getting Started

Official docs samples, then quoted vs unquoted lengths.

Example 1 — Official Length Samples

Quoted name, unquoted weight, and empty string.

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

Matches the official docs: 14, 4, and 0. meta.inspect keeps the numbers clear in custom properties.

Example 2 — Quotes Do Not Change Length

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

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

Quote marks are storage, not content. The space in "a b" is a real character, so the length is 3.

📈 Practical Patterns

Guards, CSS sizing with ch, and truncate helpers.

Example 3 — Max-Length Guard

Branch when a label is longer than a limit.

styles.scss
@use "sass:string";

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

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

How It Works

14 > 10, so the @if branch wins. All of this is decided at compile time—browsers never see string.length.

Example 4 — Size With ch Units

Use character count to set a minimum inline size.

styles.scss
@use "sass:string";

$label: "Save";

.btn::after {
  content: $label;
  // Use length as a ch-based hint for min inline size
  min-inline-size: string.length($label) * 1ch;
}

How It Works

Save has four characters, so Sass emits 4ch. Handy for icon buttons and fixed-width chips.

Example 5 — Truncate Helper

Keep the first n characters and append an ellipsis when needed.

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 or call string.slice. Indexes for slice are 1-based, just like the rest of sass:string.

🚀 Real-World Use Cases

  • Empty checks — skip rules when string.length($s) == 0.
  • Label guards — refuse or shorten names past a max length.
  • CSS sizing — multiply length by 1ch for approximate text width.
  • Truncate helpers — pair with string.slice for chips and badges.
  • Debug tooling — print lengths with meta.inspect while building string APIs.

🧠 How Compilation Works

1

Pass a string

Call string.length($string).

Source
2

Sass counts characters

Spaces count; empty strings return 0.

Compile
3

Use the number

Compare in @if, multiply by units, or drive slice.

Emit
4

CSS ships

Browsers only see finished numbers like 14 or 4ch.

⚠️ Common Pitfalls

  • Confusing with list.length — that counts list items, not string characters.
  • Thinking quotes count"Hi" has length 2, not 4.
  • Forgetting spaces — every space adds one to the count.
  • Expecting browser runtime — the call never appears in CSS.
  • Using bare length() carelessly — prefer string.length to avoid list clashes.

💡 Best Practices

✅ Do

  • Use @use "sass:string" and string.length()
  • Guard empty strings with == 0
  • Pair with string.slice for truncate helpers
  • Remember spaces count as characters
  • Prefer Dart Sass 1.23+

❌ Don’t

  • Confuse string length with list length
  • Count quote marks as characters
  • Expect browsers to evaluate the call
  • Use bare length() next to list helpers
  • Rely on LibSass for the module API

Key Takeaways

Knowledge Unlocked

Five things to remember about string.length()

Count characters in a Sass string at compile time.

5
Core concepts
📦 02

Module

sass:string

@use
🔗 03

Result

unitless number

Output
📚 04

Empty

returns 0

Rule
05

Pair

length then slice

Pattern

❓ Frequently Asked Questions

string.length($string) returns the number of characters in $string. Example: string.length("Helvetica Neue") is 14.
Yes. Official docs: 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.
Did you know?

Official Sass docs use both "Helvetica Neue" (14) and unquoted bold (4)—a reminder that length works the same whether the string is quoted or not.

Conclusion

string.length() returns how many characters a Sass string contains. Empty strings are 0, spaces count, and quote marks do not. Use it for guards, ch-based sizing, and truncate helpers with string.slice.

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

Next: quote strings

Learn string.quote() to turn values into quoted CSS strings.

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.

5 people found this page helpful