Engee documentation
Notebook

Creating arrays of strings

String arrays store text fragments and provide a set of functions to work with them. You can index, reshape, and merge string arrays just like any other type of array. In this article we will consider some functions for working with string arrays.


Each element of the string array contains a sequence of characters 1 by n.

In [ ]:
str = "Hello, world"
Out[0]:
"Hello, world"

Let's create a matrix of strings using the [] operator.

In [ ]:
satellites = ["Ganymede" "Europa" "Callisto";"Amalthea" "Rings of Jupiter" "Leda"]
Out[0]:
2×3 Matrix{String}:
 "Ganymede"  "Europa"            "Callisto"
 "Amalthea"  "Rings of Jupiter"  "Leda"

Arrays of strings support indexing. Let's use indexing to access the first row of the str matrix.

In [ ]:
satellites[1,:]
Out[0]:
3-element Vector{String}:
 "Ganymede"
 "Europa"
 "Callisto"

Let's access the second element in the second row of str.

In [ ]:
satellites[2,2]
Out[0]:
"Rings of Jupiter"

You can determine the size of the given matrix using the function size().

In [ ]:
size(satellites)
Out[0]:
(2, 3)

Number of array elements using the function length().

In [ ]:
length(satellites)
Out[0]:
6

You can also determine how many characters are in each element of the string array. If we put a dot in front of the parentheses. This will mean that we are accessing an array element and applying the function length() to it.

In [ ]:
length.(satellites)
Out[0]:
2×3 Matrix{Int64}:
 8   6  8
 8  16  4

You can convert a set of numeric values into a string using the function string(). For example, let's get the date and time and convert the value to a string.

In [ ]:
using Dates

d = now()
string(d)
Out[0]:
"2024-05-20T12:28:05.120"

Creating empty strings

String arrays can contain both empty and missing values. An empty string contains zero characters. When an empty string is displayed, the result is a pair of double quotes with nothing inside (""). The blank string is equivalent to the NaN string for numeric arrays. It indicates where values are missing in a string array. When a missing string is displayed, the result is missing.

You can create an empty string using the function String().

In [ ]:
str = String("")
Out[0]:
""

You can create a matrix of empty rows, for example, using the function fill().

In [ ]:
str = fill("",(2,3))
Out[0]:
2×3 Matrix{String}:
 ""  ""  ""
 ""  ""  ""

To create a missing row, you need to assign the keyword missing to the variable.

In [ ]:
str = missing
Out[0]:
missing

You can create an array of strings with both empty and missing strings.

In [ ]:
str = ["" "Ram" missing]
Out[0]:
1×3 Matrix{Union{Missing, String}}:
 ""  "Ram"  missing

Use the function ismissing(), to determine which elements are strings with missing values. Note that an empty string is not a missing string.

In [ ]:
ismissing.(str)
Out[0]:
1×3 BitMatrix:
 0  0  1

Let's find space characters using the function occursin() in the string and replace them with dashes using the function replace().

In [ ]:
TF = occursin.(" ", satellites)
Out[0]:
2×3 BitMatrix:
 0  0  0
 0  1  0
In [ ]:
satellites = replace.(satellites, " " => "-")
display(satellites)
2×3 Matrix{String}:
 "Ganymede"  "Europa"            "Callisto"
 "Amalthea"  "Rings-of-Jupiter"  "Leda"

Splitting, merging and sorting an array of strings

Merge strings into an array of strings just as you would merge arrays of any other type.

In [ ]:
str1 = ["a","b","c"];
str2 = ["d","e","f"];
str3 = ["g","h","i"];
str = [str1 str2 str3]
Out[0]:
3×3 Matrix{String}:
 "a"  "d"  "g"
 "b"  "e"  "h"
 "c"  "f"  "i"

The permutedims() function allows you to transpose matrices with row elements.

In [ ]:
str = permutedims(str)
Out[0]:
3×3 Matrix{String}:
 "a"  "b"  "c"
 "d"  "e"  "f"
 "g"  "h"  "i"

To add text to the rows, use the * operator. The operator adds text to the rows, but does not change the size of the row array.

In [ ]:
Name = ["Mary", "John", "Elizabeth", "Paul", "Ann"]
Name = [name * " Smith" for name in Name]
Out[0]:
5-element Vector{String}:
 "Mary Smith"
 "John Smith"
 "Elizabeth Smith"
 "Paul Smith"
 "Ann Smith"

For example, let's combine an array of first and last names.

In [ ]:
Name = ["Mary", "John", "Elizabeth", "Paul", "Ann"];
Lastname = ["Jones", "Adams", "Young", "Burns", "Spencer"];
full_names = Name .* " " .* Lastname
Out[0]:
5-element Vector{String}:
 "Mary Jones"
 "John Adams"
 "Elizabeth Young"
 "Paul Burns"
 "Ann Spencer"

But there is also a split function split(). It can be used to split string elements of an array.

In [ ]:
full_names = split.(full_names)
Out[0]:
5-element Vector{Vector{SubString{String}}}:
 ["Mary", "Jones"]
 ["John", "Adams"]
 ["Elizabeth", "Young"]
 ["Paul", "Burns"]
 ["Ann", "Spencer"]

Sorting of string elements can be done by the function sort().

In [ ]:
sort(Name)
Out[0]:
5-element Vector{String}:
 "Ann"
 "Elizabeth"
 "John"
 "Mary"
 "Paul"

In addition to the above examples of working with string arrays, there are a number of other functions. To learn more about the functionality, see Arrays.