"

4 String Operations

Working With Strings: Concatenation, Length, Upper/Lowercase Methods

Hasan Malik and Rutwa Engineer

Learning Objectives

  • Learn about the following String Operations:
    • Concatenation
    • Length
    • Upper/Lowercase methods
    • Slicing (via the .sub method)
  • Be introduced to basic string methods
  • Learn about two notation styles for string methods in Lua

Introduction

We’ve seen a lot of operations on numbers in Lua, e.g. addition, division, exponents, modulus, etc. Are there any operations on strings?

The answer is YES! Let’s dive in 🙂

String Concatenation

The first operation we will discuss is string concatenation. Think of concatenation as addition for strings.

String Concatenation Example

first_name = “Ali”

last_name = “Khan”

full_name = first_name .. last_name

print(full_name)

There’s a couple of interesting things to note here.

Firstly, look at how we use variables. This is a simple example of why variables are so useful in storing data. Also, notice how we use clear variable names that indicate to the reader what the data represents.

Now, what is the output of this program?

Output:

AliKhan

The first_name has been “added” to the last_name. We say that they have been concatenated.

Note that there is no space between the first name and last name, because Lua concatenates the two strings together without spacing them.

If we want to create a space, we can try several strategies:

Strategy 1

  1. Change first_name to “Ali “ or change last_name to “ Khan”, thus adding a space.

Strategy 2

Concatenate a space in the middle, when assigning full_name:

full_name = first_name .. “ “ .. last_name

If we use either of these two strategies, the output will be:

Ali Khan

Try more examples of string concatenation to get used to it!

Exercise

Bonus: Try to change Line 3 to make the output be “Berlin, Germany” (with a comma and a space!)

String Length

To find the length of a string, we use the # operator.

String Length Example

name = “Hasan”

print(#name)


Output:

5

This means that the length of the string name is 5, i.e. the string contains 5 characters.

Common Mistake

In the length of a string as determined by the # operator, symbols, digits, and spaces are all included.

Exercise

Uppercase and Lowercase

In Lua, we can use the built-in methods upper and lower to make a string all-uppercase or all-lowercase.

Example

mystr = "I love pizza!"

print(mystr:upper())

print(mystr:lower())


Output:

I LOVE PIZZA!

i love pizza!

Our variable here is named mystr, which is a common abbreviation for simple string variables (i.e. “my string”).

Using String Methods

The way to use a method is to say:

<variable> : <method name> ()

So, in the above example, where our variable is mystr and the method is upper, we wrote:

mystr:upper()

You can also use methods to assign values to a new variable:

Example

name = "Ifedayo Okonkwo"

upper_name = name:upper()

print(upper_name)


Output:

“IFEDAYO OKONKWO”

Exercise

Study the previous example.

Try doing the same with :lower()!

Both :lower() and :upper() are examples of string methods. We will see more examples of methods, not just for strings but also for other data types, as we progress through the modules.

Alternative Notation

There is an alternative way of calling a string method that you should be aware of. Remember our “I love pizza!” example above? We could also have written it like this:

Example

mystr = "I love pizza!"

print(string.upper(mystr))

print(string.lower(mystr))

This would also result in the same output:

Output:

I LOVE PIZZA!

i love pizza!

Both notations yield exactly the same result.

Alternative Notation

The format for this style of notation is:

<data type> . <method name> (<variable>)

So, in the above example, where our data type is string, the method name is upper, and the variable is mystr, we wrote:

string.upper(mystr)

It is up to you which style of notation to use, but you should be aware that both are perfectly valid.

String Slicing (.sub)

String slicing is a way of extracting part of a string (called a substring) from the whole string.

Example

Let’s say we have a variable mystr = “ABCDEFGHIJKL”, and we only want to print the letters from C onwards.

C is the third letter of the alphabet. We could say:

print(string.sub(mystr,3))


Output:

CDEFGHIJKL

Essentially, the .sub method is cutting short the string.

In the statement string.sub(mystr,3), we are telling the .sub method that we only want the part of mystr starting from index 3. Note that index means: the position of a character in a string, starting from 1. So, in mystr, A has index 1, B has index 2, all the way until L, which has index 12.

Thus, the print statement outputs all characters of mystr starting from C.

Exercise

Now, let’s revisit our initial string,  mystr = “ABCDEFGHIJKL”.

We want to print all the letters from C to H. H is the 8th letter of the alphabet. How can we achieve this?

To do this, we use the following syntax:

Example

print(string.sub(mystr,3,8))


Output:

CDEFGH

Here, we are telling the .sub method that the starting index is 3, and the ending-index is 8. The .sub method’s job is to return all letters between the starting-index and ending-index.

If you don’t type in any ending-index (as seen in Examples 1 and 2), then Lua automatically assumes the index of the last character of the string to be the ending-index.

Now, .sub is another example of a string method. Can you think of any other string methods we have discussed?

That’s right, .lower and .upper! Similar to those, .sub can also be written in the same alternative notation as all string methods. For example, the statement,

print(mystr:sub(3))

based on the Alternative Notation, would result in the same output as in the first example of this section.

Exercise

For all these exercises, assume mystr = “ABCDEFGHIJKL”.

Congratulations—now, you know quite a bit about the basic string operations in Lua!

 

License

Learn Coding and Animations in Lua Code Copyright © by Kyra Ma; Hasan Malik; and Rutwa Engineer. All Rights Reserved.