string.insert($string, $insert, $index) returns a copy of $string with $insert placed at $index. Indexes are 1-based (or negative from the end). Load with @use "sass:string".
Official sample idea.
insert("Roboto Bold", " Mono", 7) and insert(…, -6) both become "Roboto Mono Bold".
Inserts a copy of text at a 1-based or negative index.
Out-of-range clamps to prepend or append.
Pairs with index when the insert point is dynamic.
Prefer @use "sass:string" (Dart Sass 1.23+).
One line: to splice text into a string, use string.insert($string, $insert, $index).
Practice
Compile Notes
Practical checklist:
checklist.txt
1. @use "sass:string"
2. Pass $string, $insert, $index
3. Remember indexes are 1-based / negative
4. Capture the returned copy
5. Use a huge index to append, or index 1 to prepend
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".
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.