Syntax

list llParseString2List( string src, list separators, list spacers );

Forced delay: 0.0 seconds. Energy: 10.0.

Parameters

string src
source string
list separators
separators to be discarded
list spacers
spacers to be kept

In most situations llParseStringKeepNulls should be used instead. Discarding null values is rarely desired.

When dealing with vector and rotation data, consider using llCSV2List instead, since it can correctly parse them.

Return value

Returns a list that is src broken into a list of strings, discarding separators, keeping spacers, discards any null (empty string) values generated.

Caveats

Examples

default
{
    state_entry()
    {
        // This will say:
        // <A><crazy><fox><.><Saw><the><moon><.><.>
        string my_string = "A crazy fox.  Saw the moon..";
        list my_list = llParseString2List(my_string,[" "],["."]);
        llOwnerSay("<" + llDumpList2String(my_list,"><") + ">");
        
        // This will say:
        //  <A><crazy><fox><.><><><Saw><the><moon><.><><.><>
        my_list = llParseStringKeepNulls(my_string,[" "],["."]);
        llOwnerSay("<" + llDumpList2String(my_list,"><") + ">");
    }
}

While LSL does not support lists-in-lists, you can emulate lists-in-lists by making successive calls or you could use JSON.

In this example some items have additional information supplied.

   
string shoppinglist = "macaroni::pepperoni::bread#wheat::sausage#italian::coffee::syrup::apple::ice cream#strawberry#chocolate#vanilla";

default
{
    state_entry()
    {
        list items = llParseString2List(shoppinglist, ["::"], []);
        integer i = 0;
        integer j = llGetListLength(items);
        for(;i < j; ++i)
        {
            list desc = llParseString2List(llList2String(items, i), ["#"], []);
            if(llGetListLength(desc) > 1)
            {
                list types = llDeleteSubList(desc,0,0);
                llOwnerSay("Item: "+ llList2String(desc, 0) + "  Type: " + llList2CSV(llDeleteSubList(types,-2,-1) + llDumpList2String(llList2List(types,-2,-1), " & ")));
            } else {
                llOwnerSay("Item: "+ (string)desc);
            }
        }
    }
}

Useful snippets

Examples of processing more than 8 spacers or separators:

ParseString2ListFunctions exactly the same as llParseString2List and llParseStringKeepNulls.
separateWordsFunctions exactly the same as llParseString2List unless you violate it's additional preconditions.

Appears to be correct at a glance.

Notes

Whenever you need to process the resulting list entries further (eg. typecasting), remember to llStringTrim any whitespace characters from the string. This can prevent issues such as unexpected values for vectors and rotations.


If you indicate some items as separators, it will split the string where it finds the indicated separators, and strip out the separators.

If instead you indicate some items as spacers, it will split the string where it finds the spacers, but leave the spacers there, including them as separate entries in the result list.

string myString = "What Are You Looking At?";

llSay(0, llList2CSV( llParseString2List(myString,  ["W", "A", "Y", "L"], [] ) ) );
//returns:  hat , re , ou , ooking , t?
    
llSay(0, llList2CSV( llParseString2List(myString, [], ["W", "A", "Y", "L"] ) ) );
//returns: W, hat , A, re , Y, ou , L, ooking , A, t?

Using " " as a separator will parse a sentence into words.

If there is no spacer you care about, just use [] as the spacer.

If an empty string is used as a separator or a spacer, it will have no effect.

See also

Functions

Articles

Deep notes

History

Footnotes

  1. Early release notes were not very accurate or thorough, they sometimes included information about features added in previous releases or failed to include information about features added in that release.