Smashing strings together

Unlike with numbers, you can’t subtract, divide, or multiply strings. But you can add strings (which is technically known as concatenating strings). To concatenate two strings, you use the plus sign (+) to essentially smash two strings into a single string, as the following Liberty BASIC example shows:
MyName$ = “Joe Smith” ?1
PRINT “Hello, ” + MyName$ ?2
END ?3
This Liberty BASIC program tells the computer to do the following:
-
?1 Creates a string variable called MyName$ and stuffs it with the string “Joe Smith”.
-
?2 Prints the string “Hello, “ followed by the string stored in the MyName$ variable, so the entire line prints out Hello, Joe Smith.
-
?3 Tells the computer that the program is at an end.
Remember If you concatenate strings, make sure that you leave a space between the two strings so that they don’t appear smashed together (likethis). In the previous example, notice the space in the second line following the word Hello and the comma.
Both REALbasic and C++ also let you use the plus sign (+) to concatenate strings. However, Revolution uses the dual ampersand symbols (&&) to concatenate strings like so:
put “Joe Smith” into MyName
put “Hello, ” && MyName into message
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply