Difference between revisions of "StringTruncate"

From Second Life Wiki
Jump to navigation Jump to search
(Created page with 'Not to be confused with LlStringTrim. this function will trim a string if it is too long. '''Function''' <lsl> string trimstring(string text, integer length) { length ...')
 
m (<lsl> tag to <source>)
 
(10 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Not to be confused with [[LlStringTrim]].
Not to be confused with [[llStringTrim]].


this function will trim a string if it is too long.
this function will trim a string if it is too long.


'''Function'''
<source lang="lsl2">
string StringTruncate(string text, integer length)
{
    if (length < llStringLength(text))
        return llGetSubString(text, 0, length - 2) + "…";


'''Function'''
     // else
<lsl>
        return text;
string trimstring(string text, integer length) {
     length = length-1;
    string newstring = llGetSubString(text,0, length) + "...";
    return newstring;
}
}
</lsl>
</source>


'''Example'''
'''Example'''
<lsl>
<source lang="lsl2">
string trimstring(string text, integer length) {
string StringTruncate(string text, integer length)
     length = length-1;
{
    string newstring = llGetSubString(text,0, length) + "...";
     if (length < llStringLength(text))
     return newstring;
        return llGetSubString(text, 0, length - 2) + "";
 
     // else
        return text;
}
}
 
default
default
{
{
     state_entry()
     state_entry()
     {
     {
       // llSay(0, "Hello, Avatar!");
       // llSay(PUBLIC_CHANNEL, "Hello, Avatar!");
     }
     }
 
     touch_start(integer total_number)
     touch_start(integer num_detected)
     {
     {
         llSay(0,trimstring("Thisismyname Ulrik",11));
         llSay(PUBLIC_CHANNEL, StringTruncate("my name Ulrik Ulrik", 11));
     }
     }
}
}
</lsl>
</source>
{{LSLC|User-Defined Functions}}

Latest revision as of 14:38, 22 January 2015

Not to be confused with llStringTrim.

this function will trim a string if it is too long.

Function

string StringTruncate(string text, integer length)
{
    if (length < llStringLength(text))
        return llGetSubString(text, 0, length - 2) + "…";

    // else
        return text;
}

Example

string StringTruncate(string text, integer length)
{
    if (length < llStringLength(text))
        return llGetSubString(text, 0, length - 2) + "…";

    // else
        return text;
}
 
default
{
    state_entry()
    {
       // llSay(PUBLIC_CHANNEL, "Hello, Avatar!");
    }
 
    touch_start(integer num_detected)
    {
        llSay(PUBLIC_CHANNEL, StringTruncate("my name Ulrik Ulrik", 11));
    }
}