Home Sass String string.index() Sass string.index() Definition
Definition and Usage string.index($string, $substring) returns the first 1-based index of $substring in $string, or null if it is not found. Load with @use "sass:string".
Official sample idea. string.index("Helvetica Neue", "Helvetica") → 1; index(…, "Neue") → 11.
1-based indexes. The first character is at index 1, not 0—same convention as other Sass string helpers.
Missing = null. Guard with @if or compare to null before using the result with string.slice() .
Syntax
Syntax @use "sass:string";
string.index($string, $substring) //=> number | null
string.index("Helvetica Neue", "Helvetica"); // 1
string.index("Helvetica Neue", "Neue"); // 11
string.index("Helvetica Neue", "Arial"); // null
// Contains check:
string.index($haystack, $needle) != null
// Safe branch:
@if string.index($haystack, $needle) { ... }
// Global alias (legacy): str-index($string, $substring)
// Prefer string.index after @use "sass:string" Pattern: search for a substring → get a 1-based index or null → branch or feed into slice.
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. Slice 3. Contains 4. Guard 5. Paths Example 1 — Official Index Samples Find positions, and inspect null when missing.
@use "sass:meta";
@use "sass:string";
.probe {
--helvetica: #{string.index("Helvetica Neue", "Helvetica")};
--neue: #{string.index("Helvetica Neue", "Neue")};
--miss: #{meta.inspect(string.index("Helvetica Neue", "Arial"))};
} .probe {
--helvetica: 1;
--neue: 11;
--miss: null;
} How It Works Helvetica starts at index 1.Neue starts at index 11.Arial is missing → null.meta.inspect prints null safely into CSS.Example 2 — Split on a Dash with slice Find -, then cut prefix and suffix.
@use "sass:string";
$class: "btn-primary";
$pos: string.index($class, "-");
.slice {
--dash-at: #{$pos};
--prefix: #{string.slice($class, 1, $pos - 1)};
--suffix: #{string.slice($class, $pos + 1)};
} .slice {
--dash-at: 4;
--prefix: btn;
--suffix: primary;
} How It Works index finds the dash at 4.slice left takes characters before the dash.slice right takes characters after it.Pair with slice for simple token splits.Example 3 — Contains Helper Turn index into a clear boolean.
@use "sass:string";
@function contains($haystack, $needle) {
@return string.index($haystack, $needle) != null;
}
.check {
--has-neue: #{contains("Helvetica Neue", "Neue")};
--has-arial: #{contains("Helvetica Neue", "Arial")};
} .check {
--has-neue: true;
--has-arial: false;
} How It Works Any number means the substring was found.null means it was missing.!= null turns that into a boolean.Reusable for mixin and function guards.Example 4 — Guard with @if Only emit CSS when the substring exists.
@use "sass:string";
@mixin if-contains($haystack, $needle) {
@if string.index($haystack, $needle) {
.match {
content: "#{$needle}";
}
}
}
@include if-contains("card is-active", "active");
@include if-contains("card is-active", "disabled"); .match {
content: "active";
} How It Works First include finds active.Second include returns null.@if skips the missing case.Useful for optional design-system flags.Example 5 — Detect Folders in Paths Check whether a path string contains /.
@use "sass:meta";
@use "sass:string";
$paths: "icons/home.svg", "logo.png", "fonts/Roboto.woff2";
@each $path in $paths {
$slash: string.index($path, "/");
.file {
--path: #{$path};
--has-folder: #{meta.inspect($slash != null)};
--slash-at: #{meta.inspect($slash)};
}
} .file {
--path: icons/home.svg;
--has-folder: true;
--slash-at: 6;
}
.file {
--path: logo.png;
--has-folder: false;
--slash-at: null;
}
.file {
--path: fonts/Roboto.woff2;
--has-folder: true;
--slash-at: 6;
} How It Works @each scans every path./ present → report the index.Flat filenames return null.Same helper works for any delimiter search. Edge Cases & Pitfalls Most index bugs are 0-based thinking or forgetting null.
1-based First character is 1 Do not treat Sass string indexes like JavaScript’s 0-based indexes.
null Missing returns null Guard before math or slice, or compare with != null.
first Only the first match Repeated substrings still return the earliest index.
inspect Print null with meta.inspect Use meta.inspect when dumping null into CSS custom properties.
Toolchain
Sass Support Prefer the module form with current Dart Sass.
API Status string.index()Dart Sass 1.23+ via @use "sass:string" Return type Number (1-based) or null Legacy global str-index($string, $substring)
Compile with the Dart Sass CLI .
Remember
Key Takeaways Returns a 1-based index of the first substring match.
Missing = null — guard before using the number.
Pairs with slice for prefix/suffix splits.
Prefer @use "sass:string" (Dart Sass 1.23+).
One line: to find where a substring starts, use string.index($string, $substring).
Practice
Compile Notes Practical checklist:
1. @use "sass:string"
2. Pass $string and $substring
3. Expect a 1-based index or null
4. Guard before math / slice
5. Use != null for contains checks Frequently Asked Questions What does Sass string.index() do? string.index($string, $substring) returns the first index of $substring in $string, or null if it is not found. Example: string.index("Helvetica Neue", "Helvetica") is 1.
Are indexes 0-based or 1-based? Sass string indexes are 1-based. The first character is at index 1, not 0.
What if the substring is missing? The function returns null when $string does not contain $substring.
How do I check if a string contains text? Compare the result to null: string.index($haystack, $needle) != null. Or use @if string.index(...) { ... }.
Is there a global str-index()? Yes. str-index($string, $substring) is the legacy global name. Prefer string.index 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.insert() Insert text into a string at a 1-based index.
string.insert() → 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