In this tutorial, you’ll learn how to convert a Julia string to an array. Converting string to array is a common programming question in many programming languages. While getting started with Julia this was something I learnt first while learning Julia.

Define a Function In Julia

Let’s start by defining a function in Julia which reads a string as paramter.

function JuliaStringToArray(input)
    
end

Convert Julia String To Array

For converting the Julia string to array, make use of the split function which splits the string based on the separator.

function JuliaStringToArray(input)
    arr = split(input,"")
    println(arr)
end
JuliaStringToArray("HELLO")

Try running the code and the string will be converted to an array.

# output
SubString{String}["H", "E", "L", "L", "O"]

Let’s try printing the array using a for loop.

function JuliaStringToArray(input)
    arr = split(input,"")
    println(arr)
    for i = 1:length(arr)
        println(arr[i])
    end
end
JuliaStringToArray("HELLO")

For loop iterates from 1 to the length of the arr array. Save the changes and try executing the Julia code.

The above code will return the following output:

SubString{String}["H", "E", "L", "L", "O"]
H
E
L
L
O

Final Thoughts

In this quick tutorial, you learnt how to convert a julia string to array. You also learnt how to print the array items using a for loop.

Do let us know your thoughts in the comments below.