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
Concept
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".
Foundation
📝 Syntax
styles.scss
@use "sass:string";
// Recommended
string.insert($string, $insert, $index)
// Legacy global name
str-insert($string, $insert, $index)
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".
Watch Out
⚠️ 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.
Pro Tips
💡 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
Summary
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
📝01
API
insert(s, piece, i)
Call
📦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.
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.