Difference between revisions of "LlGetSubString"

From Second Life Wiki
Jump to navigation Jump to search
m (added SL clock example to show how llGetSubString can make if-clauses unnecessary)
(Simplified 2nd example, removing inappropriate use of llFloor() and using % operator, saving 140 bytes of Mono ByteCode in the process)
Line 36: Line 36:
     timer()
     timer()
     {
     {
         integer seconds = llFloor(llGetWallclock());
         integer seconds = llGetWallclock();
         integer minutes = llFloor(seconds / 60);
         integer minutes = seconds / 60;
         seconds = seconds - (minutes * 60);
         seconds = seconds % 60;
         integer hours = llFloor(minutes / 60);
         integer hours = minutes / 60;
         minutes = minutes - (hours * 60);
         minutes = minutes % 60;


         string stringHours = llGetSubString("0" + (string)hours, -2, -1);
         string stringHours   = llGetSubString("0" + (string)hours,   -2, -1);
         string stringMinutes = llGetSubString("0" + (string)minutes, -2, -1);
         string stringMinutes = llGetSubString("0" + (string)minutes, -2, -1);
         string stringSeconds = llGetSubString("0" + (string)seconds, -2, -1);
         string stringSeconds = llGetSubString("0" + (string)seconds, -2, -1);
Line 48: Line 48:
         string time = stringHours + ":" + stringMinutes + ":" + stringSeconds;
         string time = stringHours + ":" + stringMinutes + ":" + stringSeconds;


         llSetText(time, <1.0, 1.0, 1.0>, (float)TRUE);
         llSetText(time, <1.0, 1.0, 1.0>, 1.0);
     }
     }
}
}

Revision as of 12:56, 3 January 2013

Summary

Function: string llGetSubString( string src, integer start, integer end );
0.0 Forced Delay
10.0 Energy

Returns a string that is the substring of src from start to end, leaving the original string intact.

• string src
• integer start start index
• integer end end index

start & end support negative indexes.

Specification

Index Positive Negative
First 0 -length
Last length - 1 -1

Indexes

  • Positive indexes count from the beginning, the first item being indexed as 0, the last as (length - 1).
  • Negative indexes count from the far end, the first item being indexed as -length, the last as -1.

Caveats

  • If either start or end are out of bounds the script continues to execute without an error message.
  • start & end will form an exclusion range when start is past end (Approximately: start > end).

Examples

<lsl> default {

   state_entry()
   {
       string word = "Hello!";
       llOwnerSay(llGetSubString(word, 0, 0));
       // Object: H
       llOwnerSay(llGetSubString(word, -1, -1));
       // Object: !
       llOwnerSay(llGetSubString(word, 2, 3));
       // Object: ll
   }

} </lsl> <lsl> // display SL time by using a script without a single if! default {

   state_entry()
   {
       llSetTimerEvent(1.0);
   }
   timer()
   {
       integer seconds = llGetWallclock();
       integer minutes = seconds / 60;
       seconds = seconds % 60;
       integer hours = minutes / 60;
       minutes = minutes % 60;
       string stringHours   = llGetSubString("0" + (string)hours,   -2, -1);
       string stringMinutes = llGetSubString("0" + (string)minutes, -2, -1);
       string stringSeconds = llGetSubString("0" + (string)seconds, -2, -1);
       string time = stringHours + ":" + stringMinutes + ":" + stringSeconds;
       llSetText(time, <1.0, 1.0, 1.0>, 1.0);
   }

}

</lsl>

Notes

Ranges & Indexes

The easiest way to explain how ranges work is to make all indexes positive. Negative indexes are just a way of counting from the tail end instead of the beginning, all negative indexes have a corresponding equivalent positive index (assuming they are in range). Positive indexes past length (after the last index), or negative indexes past the beginning (before the first index) are valid and the effects are predictable and reliable: the entries are treated as if they were there but were removed just before output.

  • If start <= end then the range operated on starts at start and ends at end. [start, end]
  • Exclusion range: If start > end then the range operated on starts at 0 and goes to end and then starts again at start and goes to -1. [0, end] + [start, -1]
    • If end is a negative index past the beginning, then the operating range would be [start, -1].
    • If end is a positive index past the end, then the operating range would be [0, end].
    • If both start and end are out of bounds then the function would have no operating range (effectively inverting what the function is supposed to do).

See negative indexes for more information. The counting of the characters starts at 0. Using 0,0 as start and end positions would return the first character only. Using negative numbers causes backwards counting, so -1 is shortform for the last character in a string. Ergo, using 0, -1 as start and end positions would return the entire string.

To ascertain how long a string is, use llStringLength.

See Also

Deep Notes

Signature

function string llGetSubString( string src, integer start, integer end );