Sass Strings
Sass has two kinds of strings: quoted ("Inter", 'icon') and unquoted (bold, sans-serif, flex). Most CSS keywords are unquoted strings. The sass:string module lets you search, slice, combine and transform strings — useful for building class names, file paths and content values.
Quoted and unquoted
@use "sass:string";
$family: "Fira Code";
$weight: bold;
code {
font-family: $family, monospace;
font-weight: $weight;
}
.demo {
a: string.unquote("flex");
b: string.quote(grid);
c: "pre" + fix;
d: pre + "fix";
}code {
font-family: "Fira Code", monospace;
font-weight: bold;
}
.demo {
a: flex;
b: "grid";
c: "prefix";
d: prefix;
}
When you combine strings with +, the result is quoted if the first string was quoted.
The sass:string functions
| Function | Example | Result |
|---|---|---|
string.length($s) |
string.length("sass") |
4 |
string.index($s, $sub) |
string.index("btn-lg", "-") |
4 (1-based) |
string.slice($s, $start, $end) |
string.slice("button", 1, 3) |
"but" |
string.insert($s, $ins, $i) |
string.insert("bt", "u", 2) |
"but" |
string.to-upper-case($s) |
string.to-upper-case("ok") |
"OK" |
string.to-lower-case($s) |
string.to-lower-case("OK") |
"ok" |
string.quote($s) / string.unquote($s) |
add / remove quotes | |
string.unique-id() |
a random identifier (different each compile) |
Like lists, strings are 1-indexed.
@use "sass:string";
$name: "btn-primary-lg";
.info {
--length: #{string.length($name)};
--dash-at: #{string.index($name, "-")};
--block: #{string.slice($name, 1, 3)};
--after-block: #{string.slice($name, 5)};
--shout: #{string.to-upper-case($name)};
}.info {
--length: 14;
--dash-at: 4;
--block: btn;
--after-block: primary-lg;
--shout: BTN-PRIMARY-LG;
}
Practical: a prefix-stripping function
Split a BEM-style name into its parts by finding the separator:
@use "sass:string";
@function strip-prefix($name, $prefix) {
@if string.index($name, $prefix) == 1 {
@return string.slice($name, string.length($prefix) + 1);
}
@return $name;
}
@each $token in "color-brand", "color-ink", "space-4" {
.#{strip-prefix($token, "color-")} {
--token: #{$token};
}
}.brand {
--token: color-brand;
}
.ink {
--token: color-ink;
}
.space-4 {
--token: space-4;
}
Practical: content values and escapes
The content property needs a quoted string. Interpolate values into it:
$required-mark: "*";
$sep: "/";
.label.required::after {
content: " #{$required-mark}";
color: #c62828;
}
.breadcrumb li + li::before {
content: "#{$sep}";
padding: 0 6px;
}.label.required::after {
content: " *";
color: #c62828;
}
.breadcrumb li + li::before {
content: "/";
padding: 0 6px;
}
Tip: Avoid generating selectors from long, sliced strings when a map would express the same thing directly. String functions are best for small transformations like prefixes and case changes.
Test yourself
What does string.slice("layout", 1, 3) return?
- "lay"
- "ay"
- "ayo"
- "la"
What is the result of "icon" + -home?
- icon-home (unquoted)
- "icon-home" (quoted)
- An error
- "icon" -home
Which function removes quotes from a string?
- string.strip()
- string.unquote()
- string.raw()
- string.slice()