Sass string.insert() Function

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

What You’ll Learn

string.insert() belongs to the built-in sass:string module. It returns a copy of a string with new text inserted at a 1-based or negative index. This page covers out-of-range clamping, font-name building, and five compiled examples.

01

Concept

Insert at an index

02

Module

@use "sass:string"

03

Returns

New string

04

Args

string, insert, index

05

Indexes

+ / − / clamp

06

Practice

5 examples

What Is string.insert()?

Official docs: returns a copy of $string with $insert inserted at $index. Positive indexes count from the start; negative indexes count from the end.

  • The original string is not mutated—you always get a new value.
  • If $index is past the end, the insert is appended.
  • If $index is before the start (too negative), the insert is prepended.
  • Pair with string.index when you need to find a position first.
💡
Beginner tip

Picture sliding a sticky note into a word. In "Roboto Bold", inserting " Mono" at index 7 builds "Roboto Mono Bold".

📝 Syntax

styles.scss
@use "sass:string";

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

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

Parameters

ParameterTypeRequiredDescription
$stringStringYesThe base string to copy.
$insertStringYesText to insert.
$indexNumberYes1-based or negative insert position.

Return value

TypeResult
StringA new string with $insert placed at $index

📦 Loading sass:string

styles.scss
// Recommended — namespaced (Dart Sass 1.23+)
@use "sass:string";
$name: string.insert("Roboto Bold", " Mono", 7); // "Roboto Mono Bold"

// Optional — bring members into scope
@use "sass:string" as *;
$name: insert("Roboto Bold", " Mono", 7);

// Legacy global
$name: str-insert("Roboto Bold", " Mono", 7);

🎨 Official Patterns

CallResult
insert("Roboto Bold", " Mono", 7)"Roboto Mono Bold"
insert("Roboto Bold", " Mono", -6)"Roboto Mono Bold"
insert("Roboto", " Bold", 100)"Roboto Bold" (clamped to end)
insert("Bold", "Roboto ", -100)"Roboto Bold" (clamped to start)

Positive and negative indexes can describe the same insertion point. Huge indexes safely clamp instead of throwing.

⚖️ Positive vs Negative Indexes

Index styleCounts fromOut of range
PositiveStart of the stringAppends at the end
NegativeEnd of the stringPrepends at the beginning

🛠 Compatibility

This is about Sass compilers, not browsers. Insertion happens at compile time.

Implementationinsert
Dart SassYes — prefer string.insert (module since 1.23.0)
LibSassLegacy str-insert may exist; no @use "sass:string"
Ruby SassLegacy str-insert 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";
Insert in the middlestring.insert("Roboto Bold", " Mono", 7)
Insert from the endstring.insert($s, $piece, -6)
Append safelystring.insert($s, $piece, 100)
Prepend safelystring.insert($s, $piece, -100)
Find then insertstring.insert($s, $piece, string.index($s, " "))

Examples Gallery

Each example uses string.insert at compile time (Dart Sass 1.23+). Open View Compiled CSS for verified output.

📚 Getting Started

Official docs samples, then a real font-family rule.

Example 1 — Official Insert Samples

Positive index, negative index, and both clamp cases.

styles.scss
@use "sass:meta";
@use "sass:string";

.probe {
  --pos: #{meta.inspect(string.insert("Roboto Bold", " Mono", 7))};
  --neg: #{meta.inspect(string.insert("Roboto Bold", " Mono", -6))};
  --end: #{meta.inspect(string.insert("Roboto", " Bold", 100))};
  --start: #{meta.inspect(string.insert("Bold", "Roboto ", -100))};
}

How It Works

meta.inspect shows the quoted results. Positive and negative indexes can land on the same spot; extreme indexes clamp.

Example 2 — Build a Font Family Name

Insert a weight word into a family string for CSS.

styles.scss
@use "sass:string";

.heading {
  font-family: string.insert("Roboto Bold", " Mono", 7), sans-serif;
}

How It Works

The quoted result is valid CSS for font-family—quotes stay in the output.

📈 Practical Patterns

Prefixes, end/start clamps, and BEM-style class building.

Example 3 — Prefix Helper

Insert at index 1 to put text at the front.

styles.scss
@use "sass:string";

@function with-prefix($name, $prefix) {
  @return string.insert($name, $prefix, 1);
}

.badge {
  content: with-prefix("Sale", "Mega ");
}

How It Works

Index 1 inserts before the first character, which is a clean prepend pattern.

Example 4 — Append and Mid Insert

Clamp to the end, or splice a separator in the middle.

styles.scss
@use "sass:meta";
@use "sass:string";

$class: "btn";
$mod: "-primary";

.compare {
  --append-end: #{meta.inspect(string.insert($class, $mod, 100))};
  --insert-mid: #{meta.inspect(string.insert("iconhome", "-", 5))};
}

How It Works

A large positive index appends without measuring length. icon + - + home is a mid-string splice.

Example 5 — BEM Element Loop

Append element suffixes onto a block name.

styles.scss
@use "sass:string";

$base: "card";
$parts: ("__title", "__body", "__footer");

@each $part in $parts {
  .#{string.insert($base, $part, 100)} {
    display: block;
  }
}

How It Works

Inserting at a huge index appends each __* suffix. Interpolation into the selector drops the Sass quotes for a normal class name.

🚀 Real-World Use Cases

  • Font naming — inject weight or style words into family strings.
  • BEM tooling — append __el / --mod pieces onto a block.
  • Label builders — prepend badges or status text for content.
  • Path helpers — insert separators after a known index from string.index.
  • Safe concat — clamp indexes instead of manually checking string length.

🧠 How Compilation Works

1

Pass string, insert, index

Call string.insert($string, $insert, $index).

Source
2

Sass copies and splices

Resolves positive/negative indexes and clamps if needed.

Compile
3

Use the new string

Assign to CSS properties or interpolate into selectors.

Emit
4

CSS ships

Browsers only see finished text like "Roboto Mono Bold".

⚠️ Common Pitfalls

  • 0-based habits — Sass string indexes are 1-based.
  • Forgetting spaces — include leading/trailing spaces in $insert when you need them (" Mono").
  • Expecting mutation — the original variable stays unchanged.
  • Wrong arg order — remember string, then insert, then index.
  • Confusing with list helpers — this edits characters, not list items.

💡 Best Practices

✅ Do

  • Use @use "sass:string" and string.insert()
  • Use large indexes when you simply want append/prepend
  • Combine with string.index for dynamic positions
  • Keep intentional spaces inside $insert
  • Prefer Dart Sass 1.23+

❌ Don’t

  • Assume 0-based indexes like JavaScript
  • Expect the original string variable to change
  • Forget that out-of-range indexes clamp quietly
  • Expect browsers to evaluate the call
  • Rely on LibSass for the module API

Key Takeaways

Knowledge Unlocked

Five things to remember about string.insert()

Copy a string and splice in new text at a 1-based or negative index.

5
Core concepts
📦 02

Module

sass:string

@use
🔗 03

Result

new string copy

Output
📚 04

Indexes

+ / − / clamp

Rule
05

Pair

index then insert

Pattern

❓ Frequently Asked Questions

string.insert($string, $insert, $index) returns a copy of $string with $insert inserted at $index. Example: string.insert("Roboto Bold", " Mono", 7) becomes "Roboto Mono Bold".
Positive indexes count from the start (1 is the first character). Negative indexes count from the end. Official sample: string.insert("Roboto Bold", " Mono", -6) also yields "Roboto Mono Bold".
Official docs: if $index is higher than the string length, $insert is added to the end. If $index is smaller than the negative length, $insert is added to the beginning.
No. It returns a new string. The original value is unchanged.
Yes. str-insert($string, $insert, $index) is the legacy global name. Prefer string.insert 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 show both 7 and -6 producing "Roboto Mono Bold"—a clear demo that positive and negative indexes can target the same insertion point.

Conclusion

string.insert() copies a string and splices in new text at a 1-based or negative index. Out-of-range indexes clamp to the start or end, which makes append/prepend helpers easy. Pair it with string.index when the position is dynamic.

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

Next: count characters

Learn string.length() to measure Sass strings at compile time.

string.length() →

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