Two ways to join
Joining strings together is called concatenation. xTalk has two operators for it: & joins two values exactly as they are, and && joins them with a space between.
put "sun" & "flower"
put "sun" && "flower"
put "Room" && 101
put "Total:" && 6 * 7 && "points"
sunflower sun flower Room 101 Total: 42 points
Numbers join just like text, and a sum is worked out before it is joined. You can chain as many pieces as you like on one line.
Names for characters you cannot see
Some characters are hard to type inside quotes. xTalk gives them names, called constants, which you join like any other value:
| Constant | Is |
|---|---|
space | one space (&& is a shortcut for & space &) |
tab | a tab character, for columns |
return (or cr) | a new line |
comma, colon, slash | , : / |
quote | a double quote, " |
empty | nothing at all, the same as "" |
put "Line one" & return & "Line two"
put "Name" & tab & "Score"
put "a" & comma & "b" & comma & "c"
put "Say" && quote & "cheese" & quote
Line one Line two Name Score a,b,c Say "cheese"
Notice that the first put made two lines of Output: the value itself has a new line in it.
Building a string a piece at a time
Long text is often built in a variable, a piece at a time, with put ... after (and put ... before for the front). Variables come in the next lesson, but the idea is simple: tLine is a named place to keep the string while it grows.
put "Dear" into tLine
put space & "Ada" after tLine
put "," after tLine
put "Hello! " before tLine
put tLine
Hello! Dear Ada,
Numbers inside text
When you join a number to text, its digits become part of the string. Be careful with sums: brackets make sure the sum happens before the join.
put "2 + 3 =" && 2 + 3
put "Ages:" && (20 + 1) & "," && (30 + 1)
put 1 & 2 + 3
2 + 3 = 5 Ages: 21, 31 15
The last line shows why brackets help: + is worked out before &, so xTalk adds 2 and 3 first and then joins the 1 in front, giving the string 15.
Try it yourself
Make a single put show a two-line label: Name: Grace Hopper on the first line and Born: 1906 on the second. Use &&, & and return.
Show a solution
put "Name:" && "Grace" && "Hopper" & return & "Born:" && 1906
Name: Grace Hopper Born: 1906
Words in this lesson
Each one finds every lesson that uses it. The Dictionary has the whole story of each, with its syntax and more examples.