Difference between revisions of "LSL 101/String Concatenation"

From Second Life Wiki
Jump to navigation Jump to search
m (header/footer fixes)
m (added lsl xml tags)
Line 5: Line 5:
'''String concatenation''' allows us to combine two or more strings into a single string, as if they were simply pasted together.  Here's an example where we use string concatenation to put the region name into a complete sentence.
'''String concatenation''' allows us to combine two or more strings into a single string, as if they were simply pasted together.  Here's an example where we use string concatenation to put the region name into a complete sentence.
   
   
<lsl>default
<lsl>
default
{
{
     state_entry()
     state_entry()
Line 18: Line 19:
           llOwnerSay( "Welcome to " + llGetRegionName() + "." );
           llOwnerSay( "Welcome to " + llGetRegionName() + "." );
     }
     }
}</lsl>
}
</lsl>


The + sign, when used with strings, indicates concatenation.  This is analogous to the use of + with integers to denote integer addition.
The + sign, when used with strings, indicates concatenation.  This is analogous to the use of + with integers to denote integer addition.
Line 24: Line 26:
As in the previous example, the server executes ''llGetRegionName'' and reduces the line to the equivalent of
As in the previous example, the server executes ''llGetRegionName'' and reduces the line to the equivalent of


llOwnerSay( "Welcome to " + "Ganymede" + "." );
<lsl>
    llOwnerSay( "Welcome to " + "Ganymede" + "." );
</lsl>


It then concatenates the three strings, so it is equivalent to
It then concatenates the three strings, so it is equivalent to


llOwnerSay( "Welcome to Ganymede." );
<lsl>
    llOwnerSay( "Welcome to Ganymede." );
</lsl>


You might be thinking that an alternative to using string concatenation could be
You might be thinking that an alternative to using string concatenation could be


llOwnerSay( "Welcome to ", llGetRegionName(), "." );
<lsl>
    llOwnerSay( "Welcome to ", llGetRegionName(), "." );
</lsl>


But this won't work, because ''llOwnerSay'' expects only ''one'' string.  Another alternative,
But this won't work, because ''llOwnerSay'' expects only ''one'' string.  Another alternative,


llOwnerSay( "Welcome to ");
<lsl>
llOwnerSay( llGetRegionName() );
    llOwnerSay( "Welcome to ");
llOwnerSay( "." );
    llOwnerSay( llGetRegionName() );
    llOwnerSay( "." );
</lsl>


is legal, but it produces three separate lines of chat instead of one.
is legal, but it produces three separate lines of chat instead of one.

Revision as of 10:20, 7 November 2012

← Strings and Simple Output ↑̲  LSL 101  ̲↑ Integers →

String concatenation allows us to combine two or more strings into a single string, as if they were simply pasted together. Here's an example where we use string concatenation to put the region name into a complete sentence.

<lsl> default {

    state_entry()
    {
         // Let the object's owner know the script is working
         llOwnerSay( "Congratulations! Your script has started to execute." );
    }

    touch_start( integer num_detected )
    {
         // Announce the region where the script is running
         llOwnerSay( "Welcome to " + llGetRegionName() + "." );
    }

} </lsl>

The + sign, when used with strings, indicates concatenation. This is analogous to the use of + with integers to denote integer addition.

As in the previous example, the server executes llGetRegionName and reduces the line to the equivalent of

<lsl>

   llOwnerSay( "Welcome to " + "Ganymede" + "." );

</lsl>

It then concatenates the three strings, so it is equivalent to

<lsl>

   llOwnerSay( "Welcome to Ganymede." );

</lsl>

You might be thinking that an alternative to using string concatenation could be

<lsl>

   llOwnerSay( "Welcome to ", llGetRegionName(), "." );

</lsl>

But this won't work, because llOwnerSay expects only one string. Another alternative,

<lsl>

   llOwnerSay( "Welcome to ");
   llOwnerSay( llGetRegionName() );
   llOwnerSay( "." );

</lsl>

is legal, but it produces three separate lines of chat instead of one.

Even if everything you read here seems completely clear, your understanding will be much better if you actually write scripts as you go along. So here's a suggested exercise. The built-in function llGetDate returns a string representing today's date. Write and test a script that announces the date whenever it is clicked on.

Proceed onward to Integers or go back to Strings and Simple Output.