Difference between revisions of "LlReplaceSubString"

From Second Life Wiki
Jump to navigation Jump to search
m (remove bogus func id)
(Got rid of some example-breaking extra parentheses)
 
Line 21: Line 21:
         string ex = "red foxes, red hens, red turnips";
         string ex = "red foxes, red hens, red turnips";
         // Replace first 2 matches, starting from the left side
         // Replace first 2 matches, starting from the left side
         ex = llReplaceSubString(ex, "red", "blue", 2) );
         ex = llReplaceSubString(ex, "red", "blue", 2);
         llSay(0, ex); // Should say "blue foxes, blue hens, red turnips"
         llSay(0, ex); // Should say "blue foxes, blue hens, red turnips"
     }
     }
Line 32: Line 32:
         string ex = "red foxes, red hens, red turnips";
         string ex = "red foxes, red hens, red turnips";
         // Replace first match, starting from the right side
         // Replace first match, starting from the right side
         ex = llReplaceSubString(ex, "red", "green", -1) );
         ex = llReplaceSubString(ex, "red", "green", -1);
         llSay(0, ex); // Should say "red foxes, red hens, green turnips"
         llSay(0, ex); // Should say "red foxes, red hens, green turnips"
     }
     }
Line 43: Line 43:
         string ex = "red foxes, red hens, red turnips";
         string ex = "red foxes, red hens, red turnips";
         // Replace all matches
         // Replace all matches
         ex = llReplaceSubString(ex, "red", "yellow", 0) );
         ex = llReplaceSubString(ex, "red", "yellow", 0);
         llSay(0, ex); // Should say "yellow foxes, yellow hens, yellow turnips"
         llSay(0, ex); // Should say "yellow foxes, yellow hens, yellow turnips"
     }
     }

Latest revision as of 10:19, 30 March 2023

Summary

Function: string llReplaceSubString( string src, string pattern, string replacement_pattern, integer count );

Returns a string that is the result of replacing the first count matching instances pattern in src with replacement_pattern.

• string src
• string pattern
• string replacement_pattern
• integer count

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

Examples

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"
    }
}
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"
    }
}
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"
    }
}

See Also

Deep Notes

Search JIRA for related Issues

Signature

function string llReplaceSubString( string src, string pattern, string replacement_pattern, integer count );