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.
str = "Hello, world"
Let's create a matrix of strings using the [] operator.
satellites = ["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.
satellites[1,:]
Let's access the second element in the second row of str.
satellites[2,2]
You can determine the size of the given matrix using the function size()
.
size(satellites)
Number of array elements using the function length()
.
length(satellites)
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.
length.(satellites)
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.
using Dates
d = now()
string(d)
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()
.
str = String("")
You can create a matrix of empty rows, for example, using the function fill()
.
str = fill("",(2,3))
To create a missing row, you need to assign the keyword missing to the variable.
str = missing
You can create an array of strings with both empty and missing strings.
str = ["" "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.
ismissing.(str)
Let's find space characters using the function occursin()
in the string and replace them with dashes using the function replace()
.
TF = occursin.(" ", satellites)
satellites = replace.(satellites, " " => "-")
display(satellites)
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.
str1 = ["a","b","c"];
str2 = ["d","e","f"];
str3 = ["g","h","i"];
str = [str1 str2 str3]
The permutedims()
function allows you to transpose matrices with row elements.
str = permutedims(str)
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.
Name = ["Mary", "John", "Elizabeth", "Paul", "Ann"]
Name = [name * " Smith" for name in Name]
For example, let's combine an array of first and last names.
Name = ["Mary", "John", "Elizabeth", "Paul", "Ann"];
Lastname = ["Jones", "Adams", "Young", "Burns", "Spencer"];
full_names = Name .* " " .* Lastname
But there is also a split function split()
. It can be used to split string elements of an array.
full_names = split.(full_names)
Sorting of string elements can be done by the function sort()
.
sort(Name)
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.