Sass string.index() Function

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

What You’ll Learn

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

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.

📝 Syntax

styles.scss
@use "sass:string";

// Recommended
string.index($string, $substring)

// Legacy global name
str-index($string, $substring)

Parameters

ParameterTypeRequiredDescription
$stringStringYesThe haystack to search.
$substringStringYesThe needle to find.

Return value

TypeResult
Number1-based index of the first match
nullNo match

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

🎨 Official Patterns

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

⚖️ Index Number vs Contains Check

GoalPattern
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) { … }

🛠 Compatibility

This is about Sass compilers, not browsers. The search runs at compile time.

Implementationindex
Dart SassYes — prefer string.index (module since 1.23.0)
LibSassLegacy str-index may exist; no @use "sass:string"
Ruby SassLegacy str-index 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";
Find a matchstring.index("Helvetica Neue", "Neue")
Contains?string.index($s, $sub) != null
Print null safelymeta.inspect(string.index($s, $sub))
Slice after a matchstring.slice($s, string.index($s, "-") + 1)

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.

styles.scss
@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"))};
}

How It Works

Use meta.inspect when printing null into CSS custom properties. Successful matches interpolate as numbers.

Example 2 — Split on a Dash with slice

Find -, then cut prefix and suffix.

styles.scss
@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)};
}

How It Works

Index gives the dash position. slice uses that number to return the pieces on either side.

📈 Practical Patterns

Contains helpers, guards, and path checks.

Example 3 — Contains Helper

Turn index into a clear boolean.

styles.scss
@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")};
}

How It Works

Any number is truthy for presence; null means missing. Comparing to null keeps the API beginner-friendly.

Example 4 — Guard with @if

Only emit CSS when the substring exists.

styles.scss
@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");

How It Works

The second include returns null, so no rule is emitted. That pattern is ideal for optional design-system flags.

Example 5 — Detect Folders in Paths

Check whether a path string contains /.

styles.scss
@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)};
  }
}

How It Works

Each path is scanned for /. Flat filenames return null; nested ones report the slash index.

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

⚠️ 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".

💡 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

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

Conclusion

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.

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

Next: insert text

Learn string.insert() to place text at a 1-based or negative index.

string.insert() →

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