Home Sass String string.length() Sass string.length() Definition
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
Syntax @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.
Code
SCSS Examples Examples use @use "sass:string". Open View Compiled CSS for verified Dart Sass output (matches official Sass docs samples where shown).
1. Official 2. Quotes 3. Guard 4. ch size 5. Truncate Example 1 — Official Length Samples Quoted name, unquoted weight, and empty string.
@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(""))};
} .probe {
--quoted: 14;
--unquoted: 4;
--empty: 0;
} How It Works Helvetica Neue includes the space → 14.bold unquoted still has four characters.Empty string is 0.Matches the official Sass docs samples.Example 2 — Quotes Do Not Change Length Quoted and unquoted Roboto both count six characters; spaces still count.
@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"))};
} .compare {
--a: 6;
--b: 6;
--space: 3;
} How It Works Quotes are storage, not content.Both forms of Roboto are length 6.Space in "a b" counts.Length is 3 for that sample.Example 3 — Max-Length Guard Branch when a label is longer than a limit.
@use "sass:string";
$name: "Helvetica Neue";
$max: 10;
.card {
@if string.length($name) > $max {
content: "too-long";
} @else {
content: $name;
}
} .card {
content: "too-long";
} How It Works 14 > 10 , so the @if branch wins.Compile-time decision only.Browsers never see string.length.Useful for design-system label checks.Example 4 — Size With ch Units Use character count to set a minimum inline size.
@use "sass:string";
$label: "Save";
.btn::after {
content: $label;
min-inline-size: string.length($label) * 1ch;
} .btn::after {
content: "Save";
min-inline-size: 4ch;
} How It Works Save has four characters.Sass multiplies by 1ch.Emits 4ch in the finished CSS.Handy for chips and icon buttons.Example 5 — Truncate Helper Keep the first n characters and append dots when needed.
@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);
} .chip {
content: "VeryLong...";
} How It Works length decides whether to truncate.slice keeps the first 8 characters."..." marks the cut.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.
Toolchain
Sass Support Prefer the module form with current Dart Sass.
API Status string.length()Dart Sass 1.23+ via @use "sass:string" Return type Number (character count) Empty string 0Legacy global str-length($string)
Compile with the Dart Sass CLI .
Remember
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).
Practice
Compile Notes Practical checklist:
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 What does Sass string.length() do? string.length($string) returns the number of characters in $string. Example: string.length("Helvetica Neue") is 14.
Does the empty string return 0? Yes. string.length("") is 0.
Do quotes count as characters? 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.
Do spaces count? Yes. Spaces are characters. "a b" has length 3.
Is there a global str-length()? Yes. str-length($string) is the legacy global name. Prefer string.length after @use "sass:string".
Which compilers support the module API? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion