Creating arrays of strings
Arrays of strings store text fragments and provide a set of functions for working with them. Arrays of strings can be indexed, reshaped, and combined in the same way as arrays of any other type. In this article, we will look at 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 row matrix using the [] operator.
satellites = ["Ganymede" "Europa" "Callisto";"Amalthea" "Rings of Jupiter" "Leda"]
Arrays of strings support indexing. We use indexing to access the first row of the str matrix.
satellites[1,:]
Let's turn to the second element in the second line of str.
satellites[2,2]
You can determine the size of a given matrix using the function size().
size(satellites)
The number of array elements using the function length().
length(satellites)
You can also specify the number of characters in each element of the string array. If you put a period before the parentheses. This will mean that we access an element of the array and apply a function to it. length().
length.(satellites)
You can convert a set of numeric values to a string using the function string(). For example, we get the date and time and convert the value to a string.
using Dates
d = now()
string(d)
Creating empty lines
Arrays of strings can contain both empty and missing values. An empty string contains zero characters. When displaying an empty string, the result is a pair of double quotes with nothing inside (""). The missing string is equivalent to the NaN string for numeric arrays. It indicates where values are missing in the string array. When a missing line is displayed, the result will be missing.
You can create an empty row 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 string, assign the keyword missing to the variable.
str = missing
You can create an array of lines with both empty and missing lines.
str = ["" "Ram" missing]
Use the function ismissing() to determine which elements are strings with missing values. Note that an empty line is not a missing line.
ismissing.(str)
We will find the space characters using the function occursin() in a line and replace them with a dash with the function replace().
TF = occursin.(" ", satellites)
satellites = replace.(satellites, " " => "-")
display(satellites)
Splitting, combining, and sorting an array of strings
Combine strings into an array of strings in the same way as you would combine arrays of any other type.
str1 = ["a","b","c"];
str2 = ["d","e","f"];
str3 = ["g","h","i"];
str = [str1 str2 str3]
Function permutedims() allows you to transpose matrices with string elements.
str = permutedims(str)
To add text to lines, use the operator operator *. The operator adds text to the lines, but does not change the size of the array of lines.
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 separation function split(). It can be used to separate the string elements of an array.
full_names = split.(full_names)
The function allows you to sort string elements. sort().
sort(Name)
In addition to the examples of working with string arrays, there are a number of other functions. You can learn more about the functionality in the [Arrays] section (https://engee.com/helpcenter/stable/julia/base/arrays.html ).