Difference between revisions of "LSL 101/String Concatenation"

From Second Life Wiki
Jump to navigation Jump to search
m
m (added template & bold, footer & checked navbar)
Line 1: Line 1:
[[Category:LSL 101]]
[[Category:LSL 101]]
{{NavNextPrev|prev=Functions That Return a Value|next=Variables}}
{{NavNextPrev|prev=Variables|next=Functions}}
{{LSL Wikibook Index}}


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
Line 43: Line 44:
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|''llGetDate'']] returns a string representing today's date.  Write and test a script that announces the date whenever it is clicked on.
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|''llGetDate'']] returns a string representing today's date.  Write and test a script that announces the date whenever it is clicked on.


When you've completed that, we'll discuss a concept that is key to most programming languages -- the [[LSL 101/Variables|variable]].
Go back to [[LSL 101/Variables | Variables]] or proceed onward to [[LSL 101/Functions | Functions]].

Revision as of 21:52, 7 July 2012

← Variables ↑̲  LSL 101  ̲↑ Functions →

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

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

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

llOwnerSay( "Welcome to Ganymede." );

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

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

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

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

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.

Go back to Variables or proceed onward to Functions.