Difference between revisions of "NewLine"

From Second Life Wiki
Jump to navigation Jump to search
m (<lsl> tag to <source>)
 
(2 intermediate revisions by 2 users not shown)
Line 14: Line 14:
== Specification ==
== Specification ==
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
<lsl>
<source lang="lsl2">
string NewLine(string message)
string NewLine(string message)
{
{
Line 26: Line 26:
     return message;
     return message;
}
}
</lsl>
</source>
 
A shorter alternative:
<source lang="lsl2">
string NewLine(string message)
{
    list lWords = llParseStringKeepNulls(message, ["\\n"], []);
    return llDumpList2String(lWords, "\n");
}
</source>
</div></div>
</div></div>


<div id="box">
<div id="box">
== Example ==
== Example ==
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
Example used in a prim to generate floating text.
Example used in a prim to generate floating text.
<lsl>
<source lang="lsl2">
string NewLine(string message)
string NewLine(string message)
{
{
Line 65: Line 75:
     }
     }
}
}
</lsl>
</source>
In chat,
In chat,


Line 74: Line 84:
"/1 test\ntest\ntest" causes the prim to display:
"/1 test\ntest\ntest" causes the prim to display:


test</br>
test<br/>
test</br>
test<br/>
test</br>
test
</div></div>
</div></div>


{{LSLC|Examples|NewLine}}
{{LSLC|Examples|NewLine}}

Latest revision as of 17:31, 24 January 2015

Function: string NewLine(string source);

Returns a string with all "\n" seperators replaced by 'new line' escape codes.
The advantage of this function is the use of one integer and one string.

• string source source string

Specification

string NewLine(string message)
{
    integer newlinepos = llSubStringIndex(message, "\\n");
    while(newlinepos >= 0)
    {
        message = llDeleteSubString(message, newlinepos, newlinepos + 1);
        message = llInsertString(message, newlinepos, "\n");
        newlinepos = llSubStringIndex(message, "\\n");
    }
    return message;
}

A shorter alternative:

string NewLine(string message)
{
    list lWords = llParseStringKeepNulls(message, ["\\n"], []);
    return llDumpList2String(lWords, "\n");
}

Example

Example used in a prim to generate floating text.

string NewLine(string message)
{
    integer newlinepos = llSubStringIndex(message, "\\n");
    while(newlinepos >= 0)
    {
        message = llDeleteSubString(message, newlinepos, newlinepos + 1);
        message = llInsertString(message, newlinepos, "\n");
        newlinepos = llSubStringIndex(message, "\\n");
    }
    return message;
}

default
{
    on_rez(integer rez_param)
    {
        llResetScript();
    }

    state_entry()
    {
        llSetText("", <1,1,1>, 0.0);   
        llListen(1, "", llGetOwner(), "");
    }

    listen(integer channel, string name, key id, string str)
    {
        str = NewLine(str);        
        llSetText(str, <1,1,1>, 1.0);   
    }
}

In chat,

"/1 test test test" causes the prim to display:

test test test

"/1 test\ntest\ntest" causes the prim to display:

test
test
test