Difference between revisions of "StringTruncate"

From Second Life Wiki
Jump to navigation Jump to search
(Added an example demonstrating StringTruncate is not needed)
(Undo revision 1175319)
Line 36: Line 36:
     {
     {
         llSay(PUBLIC_CHANNEL, StringTruncate("my name Ulrik Ulrik", 11));
         llSay(PUBLIC_CHANNEL, StringTruncate("my name Ulrik Ulrik", 11));
    }
}
</lsl>
'''simpler code'''
You don't need a user function for this exercise, as you can just use llGetSubString().
If you request a length longer longer than the string actually is, it will return the full string.
(Omei Qunhua)
<lsl>
default
{
    state_entry()
    {
        string text = "abcdefghijklmnopqrstuvwxyz";
        llSay(0, "<" + llGetSubString(text, 0 , 33) + ">" );  // outputs <abcdefghijklmnopqrstuvwxyz>
        llSay(0, "<" + llGetSubString(text, 0, 9) + ">" );    // outputs <abcdefghij>
     }
     }
}
}
</lsl>
</lsl>
{{LSLC|User-Defined Functions}}
{{LSLC|User-Defined Functions}}

Revision as of 08:07, 16 December 2012

Not to be confused with llStringTrim.

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

Function <lsl> string StringTruncate(string text, integer length) {

   if (length < llStringLength(text))
       return llGetSubString(text, 0, length - 2) + "…";
   // else
       return text;

} </lsl>

Example <lsl> 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));
   }

} </lsl>