8.4. Strings#

A string in programming is a sequence of characters and is used to handle text data. A string in MATLAB is declared using single or double quotation marks, so 'hello world' is the same as "hello world".

Declaring a string variable is simply done by assigning a variable equal to a string.

variable = "string"

For example, enter the following into the console.

>> string = "hello world"

string = 

    "hello world"

8.4.1. Modifying strings#

MATLAB has the following built-in functions that can be used to modify a string.

Function

Description

upper(string)

Converts the characters of a string to uppercase

lower(string)

Converts the characters of a string to lowercase

strip(string)

Remove spaces before and after the characters in a string

replace(string, old, new)

Replaces the occurrences of old with new in string

To demonstrate these enter the following code into the console.

>> string = "    Hello world    ";
>> upper(string)

ans = 

    "    HELLO WORLD    "

>> lower(string)

ans = 

    "    hello world    "

>> strip(string)

ans = 

    "Hello world"

>> replace(string, "l", "x")

ans = 

    "    Hexxo worxd    "

8.4.2. Concatenating strings#

To concatenate (merge) two or more strings we use the + operator.

merged string = string1 + string2 

To demonstrate this enter the following into the console.

>> string1 = "hello";
>> string2 = "world";
>> merged_string = string1 + " " + string2

merged_string = 

    "hello world"

Note that we needed to include a space " " when concatenating the two words, if we didn’t do this the concatenated string would be helloworld.


8.4.3. Character arrays#

A character array is a sequence of characters that can be used to start a short piece of text. They are slightly difference to strings in the a string is a single element whereas a character array is a sequence of elements (similar to strings in Python). To convert a MATLAB string to a character array we can use the char() command.

character_array = char(string);

To demonstrate this enter the following code into the command window.

>> string = "What have the Romans ever done for us?"

string = 

    "What have the Romans ever done for us?"

>> character_array = char(string)

character_array =

    'What have the Romans ever done for us?'

Note that the character array is contained within single quotes 'string' whereas a string uses double quotes "string".

8.4.3.1. Indexing a character array#

The characters in a character array can be indexed using the character position starting at 1 for the first character (note the use of rounded brackets).

character_array(index)

To demonstrate this enter the following into the command window.

>> character_array(1)

ans =

    'W'

>> character_array(11)

ans =

    't'

Here we have printed the 1st and 11th character in character_array.

To index a range of characters in a string we use a colon to separate the first and last characters in the range.

character_array[first character index : last character index]

To demonstrate this enter the following into the console.

>> character_array(15:25)

ans =

    'Romans ever'

Here we have printed the string which consists of the 15th to the 25th character in character_array.


8.4.4. Length of a string#

The length of a string or character array is the number of characters in the string and can be determined using the length() function.

length(string)

To demonstrate this enter the following into the console.

>> length(string)

ans =

    38

So our string (and character array) is 38 characters long.


8.4.5. Exercise#

Exercise 8.3

Define two string variables for the following:

  • String 1: “Your mother was a hamster”

  • String 2: “and your father smelt of elderberries!”

Use your strings to answer the following:

  1. Print string 1 using all lowercase characters

  2. Print string 2 using all uppercase characters

  3. Print string 2 with the word “elderberries” replaced with “roses”

  4. Create another string variable by concatenating string 1 and string 2 and print it

  5. Print the length of your concatenated string (you will need to convert the string to a character array using the char() function)

  6. Print the last 30 characters of the concatenated string