Lua Logo
Strings

The frist thing I would like to mention is, that it is impossible to calculate with strings.
A string is a concatenation of numbers as we have already learned and our computer uses the according symbol when he needs to.
However, we are also able to enter not only numbers or only characters via our keyboard.
We can do this as well inside a string. But we have to tell this to the interpreter and we do this by using the backslash. \

How to enter a backslash via the keyboard depends on which language has been choosen on your computer. However, pressing the Alt-key at the same time as pressing the number 92 results in a backslash as well.
Having a look into the ASCII-Table, we can see that the value of backslash is 92. Let's try this also with other symbols.

The backslash will be followed by a special character and the interpreter knows what to do.
Special characters are as follows: (official)


  • a ? bell (PC-loudspeaker)
  • b ? back space
  • f ? form feed
  • n ? newline
  • r ? carriage return
  • t ? horizontal tab
  • v ? vertical tab
  • \ ? backslash (also a special character; otherwise he would have problems to identify himself)
  • ' ? single quote
  • " ? double quote
  • [ ? left square bracket

Let's try this with our editor. We use:
print ("wrrrt\?jhfsaldj")

Now I am going to replace the backslash and the question mark by the following special characters:

With following results:

  • \a PC - loudspeaker (does somebody still have some?). It appears a strange quadrat.
  • \b no result
  • no result at all
  • \n at least one thing that works. We tried to get a new line and we got one.
  • \r this doesn't work also
  • \t result as expected
  • also swearing doesn't help; we get no result
  • \ result as expected
  • \' result as expected
  • \" result as expected
  • \[ result as expected
  • \] result as expected

Why do we have problems by using some of these special characters?
Unfortunately, we do not have the time to solve this problem right now but perhaps these symbols can be used for other purposes!?

Let's have a look onto the last three/four special characters. They have a special relationship to strings.

With these special characters we are able to indicate the beginning and the end of a string.
And only for the purpose to start and finish a string we have to use the backslash to characterize the according sign.

If we want to emphasize a special word inside a string, we often use " " and ' '. For this purpose we need to know how to use the backslash.

If we start a string with " we need a backslash in front of the " for emphazising a special word.
And if we start a string with ' we need also a backslash in front of the ' for emphazising a special word.
If we start a string with [[ we need no backslash in front of the [ and ].

Examples:

myVar = " This is the house of \"Santa Claus\"" -- we need the backslash here
result: This is the house of "Santa Claus"

myVar = ' This is the house of "Santa Claus" ' -- we do not need the backslash in this line
result: This is the house of "Santa Claus"

myVar = ' This is the house of \'Santa Claus\' ' -- here we need the backslash again
result: This is the house of 'Santa Claus'

myVar = [[ This is the house of "Santa Claus" ]] -- no need of backslash
result: This is the house of "Santa Claus"

myVar = [[ This is the house of [Santa Claus] ]] -- no need of backslash
result: This is the house of [Santa Claus]


Moreover: if we want to integrate something that isn't included in the above mentioned list, we do not need any backslash (it would be ingnored anyway).

It is also possible to compare string:

Example:

print ("aaaaaaa" > "aaaaaab") --> false
print ("aaaaaaa" < "aaaaaab") --> true


Uff. Fortunately, we can have a look into this chapter, if we do not remind of at a later time.
The main thing is that we understand this for that moment.


-- I am going to sleep now. Tomorrow is another good day. Goodnight!

Copying of any content of this site (text or graphics) is not allowed, excepted any source code shown in this tutorial. See also: ....Disclaimer
Copyright © Robert Schmitz 2006