# llReplaceSubString

*LSL function*

```lsl
string string llReplaceSubString(string src, string pattern, string replacement_pattern, integer count);
```

- `string src`: Original source string to search.
- `string pattern`: Substring pattern to find and replace.
- `string replacement_pattern`: New substring that replaces the pattern.
- `integer count`: Maximum number of replacements to make (0 for all, positive for left-to-right, negative for right-to-left).

- Returns: `string`
- Energy: 10

If **count** = 0, all matching substrings are replaced. If **count** > 0, substrings are replaced starting from the left/beginning of **src**. If **count** &lt; 0, substrings are replaced starting from the right/end of **src**.

Returns a [string](https://wiki.secondlife.com/wiki/string) that is the result of replacing the first **count** matching instances **pattern** in **src** with **replacement_pattern**.

```lsl title="How to use" frame="terminal"
string result = llReplaceSubString("", "", "", 0);
```

## Examples

<details open>
<summary>Example 1</summary>

```lsl
default
{
    state_entry()
    {
        string ex = "red foxes, red hens, red turnips";
        // Replace first 2 matches, starting from the left side
        ex = llReplaceSubString(ex, "red", "blue", 2);
        llSay(0, ex); // Should say "blue foxes, blue hens, red turnips"
    }
}
```

</details>

<details>
<summary>Example 2</summary>

```lsl
default
{
    state_entry()
    {
        string ex = "red foxes, red hens, red turnips";
        // Replace first match, starting from the right side
        ex = llReplaceSubString(ex, "red", "green", -1);
        llSay(0, ex); // Should say "red foxes, red hens, green turnips"
    }
}
```

</details>

<details>
<summary>Example 3</summary>

```lsl
default
{
    state_entry()
    {
        string ex = "red foxes, red hens, red turnips";
        // Replace all matches
        ex = llReplaceSubString(ex, "red", "yellow", 0);
        llSay(0, ex); // Should say "yellow foxes, yellow hens, yellow turnips"
    }
}
```

</details>

## See also

### Functions

- [llGetSubString](/functions/llGetSubString/)
- [llDeleteSubString](/functions/llDeleteSubString/)
- [llInsertString](/functions/llInsertString/)
- [llDeleteSubList](/functions/llDeleteSubList/)

---

*Source: [LlReplaceSubString](https://wiki.secondlife.com/wiki/LlReplaceSubString) on the Second Life Wiki. Content from the Second Life Wiki article LlReplaceSubString (revision 1213823, 2023-03-30), CC BY-SA 3.0.*

---

From lsl.dev: https://lsl.dev/functions/llReplaceSubString/
