Difference between revisions of "StringTruncate"

From Second Life Wiki
Jump to navigation Jump to search
m (whitespace is your friend, all worship the ellipsis character :p)
m (some performance and minor readability improvements)
Line 5: Line 5:
'''Function'''
'''Function'''
<lsl>
<lsl>
string StringTruncate(string text, integer length) {
string StringTruncate(string text, integer length)
     if (length < llStringLength(text)){
{
         return llGetSubString(text,0, length - 2) + "…";
     if (length < llStringLength(text))
     }else{
         return llGetSubString(text, 0, length - 2) + "…";
 
     // else
         return text;
         return text;
    }
}
}
</lsl>
</lsl>
Line 16: Line 17:
'''Example'''
'''Example'''
<lsl>
<lsl>
string StringTruncate(string text, integer length) {
string StringTruncate(string text, integer length)
     if (length < llStringLength(text)){
{
         return llGetSubString(text,0, length - 2) + "…";
     if (length < llStringLength(text))
     } else {
         return llGetSubString(text, 0, length - 2) + "…";
 
     // else
         return text;
         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,StringTruncate("my name Ulrik Ulrik",11));
         llSay(PUBLIC_CHANNEL, StringTruncate("my name Ulrik Ulrik", 11));
     }
     }
}
}
</lsl>
</lsl>
{{LSLC|User-Defined Functions}}
{{LSLC|User-Defined Functions}}

Revision as of 11:28, 30 September 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>