string.index() belongs to the built-in sass:string module. It finds the first place a substring appears inside a string—using 1-based indexes—or returns null when it is missing. This page covers contains checks, pairing with string.slice, and five compiled examples.
01
Concept
Find a substring
02
Module
@use "sass:string"
03
Returns
Number or null
04
Args
string, substring
05
Indexes
1-based
06
Practice
5 examples
Concept
What Is string.index()?
Official docs: returns the first index of $substring in $string, or null if $string does not contain $substring.
Indexes start at 1 (the first character).
Only the first match is returned.
A missing match is null, not 0 or -1.
Pair with string.slice to cut text around the found index.
💡
Beginner tip
Think of a ruler under the string. In "Helvetica Neue", H is at 1 and Neue starts at 11.
Foundation
📝 Syntax
styles.scss
@use "sass:string";
// Recommended
string.index($string, $substring)
// Legacy global name
str-index($string, $substring)
Parameters
Parameter
Type
Required
Description
$string
String
Yes
The haystack to search.
$substring
String
Yes
The needle to find.
Return value
Type
Result
Number
1-based index of the first match
null
No match
Modules
📦 Loading sass:string
styles.scss
// Recommended — namespaced (Dart Sass 1.23+)
@use "sass:string";
$i: string.index("Helvetica Neue", "Neue"); // 11
// Optional — bring members into scope
@use "sass:string" as *;
$i: index("Helvetica Neue", "Neue");
// Legacy global
$i: str-index("Helvetica Neue", "Neue");
⚠️
Name clash tip
Prefer the namespaced call string.index. A bare index() can be confused with list.index when both modules are in scope.
Details
🎨 Official Patterns
Call
Result
index("Helvetica Neue", "Helvetica")
1
index("Helvetica Neue", "Neue")
11
Helvetica starts at the beginning (index 1). Count characters carefully for later matches like Neue.
Compare
⚖️ Index Number vs Contains Check
Goal
Pattern
Where does it start?
string.index($s, $sub) → number / null
Is it present?
string.index($s, $sub) != null → boolean
Branch in a mixin
@if string.index($s, $sub) { … }
Support
🛠 Compatibility
This is about Sass compilers, not browsers. The search runs at compile time.
Implementation
index
Dart Sass
Yes — prefer string.index (module since 1.23.0)
LibSass
Legacy str-index may exist; no @use "sass:string"
Ruby Sass
Legacy str-index may exist; no @use "sass:string"
Prefer current Dart Sass so the module API matches the official docs.
Cheat Sheet
⚡ Quick Reference
Goal
Code
Import string
@use "sass:string";
Find a match
string.index("Helvetica Neue", "Neue")
Contains?
string.index($s, $sub) != null
Print null safely
meta.inspect(string.index($s, $sub))
Slice after a match
string.slice($s, string.index($s, "-") + 1)
Hands-On
Examples Gallery
Each example uses string.index at compile time (Dart Sass 1.23+). Open View Compiled CSS for verified output.
📚 Getting Started
Official docs samples, then slice around a match.
Example 1 — Official Index Samples
Hits at the start, later in the string, and a miss.
Each path is scanned for /. Flat filenames return null; nested ones report the slash index.
Applications
🚀 Real-World Use Cases
Token parsing — find separators like - or / in names.
Feature flags — check if a class string contains is-active.
Path helpers — detect folders before generating asset URLs.
Slice pipelines — locate a point, then cut with string.slice.
Validation — require or forbid a substring before emitting CSS.
🧠 How Compilation Works
1
Pass string + substring
Call string.index($string, $substring).
Source
2
Sass searches left to right
Returns the first 1-based index, or null.
Compile
3
Branch or slice
Use @if, booleans, or string.slice.
Use
4
✓
CSS ships
Browsers only see finished values—never the search call.
Watch Out
⚠️ Common Pitfalls
0-based habits — Sass strings start at 1, not 0.
Expecting -1 on miss — missing matches are null.
Interpolating null — use meta.inspect or guard with @if.
Confusing with list.index — use the string. prefix.
Case sensitivity — "Neue" does not match "neue".
Pro Tips
💡 Best Practices
✅ Do
Use @use "sass:string" and string.index()
Treat indexes as 1-based
Compare to null for contains checks
Pair with string.slice for split-style helpers
Prefer Dart Sass 1.23+
❌ Don’t
Assume 0-based indexes like JavaScript
Treat a miss as 0 or -1
Call bare index() when list helpers are also loaded
Expect browsers to evaluate the call
Rely on LibSass for the module API
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about string.index()
Find the first 1-based index of a substring—or get null when it is missing.
5
Core concepts
📝01
API
index(str, sub)
Call
📦02
Module
sass:string
@use
🔗03
Result
number or null
Output
📚04
Base
1-based indexes
Rule
✓05
Pair
slice after find
Pattern
❓ Frequently Asked Questions
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.
Sass string indexes are 1-based. The first character is at index 1, not 0.
Official docs: the function returns null when $string does not contain $substring.
Compare the result to null: string.index($haystack, $needle) != null. Or use @if string.index(...) { ... }.
Yes. str-index($string, $substring) is the legacy global name. Prefer string.index 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 "Helvetica Neue" to show both an index-1 match and a later match at 11—a helpful reminder that Sass string positions are 1-based.
string.index() finds the first 1-based index of a substring, or returns null when it is missing. Use it for contains checks, guards, and as a partner for string.slice.