Strings and Runes in Julia
Our first example demonstrates strings and runes in Julia. Julia strings are sequences of Unicode characters, similar to UTF-8 encoded text in other languages.
# In Julia, we don't need to import packages for basic string operations
# Define a constant string containing Thai characters
const s = "สวัสดี"
# Print the length of the string in bytes
println("Len: ", sizeof(s))
# Print the hexadecimal representation of each byte in the string
for b in codeunits(s)
print("$(string(b, base=16, pad=2)) ")
end
println()
# Count the number of characters (equivalent to runes in Go)
println("Character count: ", length(s))
# Iterate over each character in the string
for (i, c) in enumerate(s)
println("$c starts at $(nextind(s, 0, i))")
end
println("\nUsing iterate function")
i = 1
for c in s
println("$c starts at $i")
global i = nextind(s, i)
end
# Define a function to examine a character
function examine_char(c::Char)
if c == 't'
println("found tee")
elseif c == 'ส'
println("found so sua")
end
end
# Call the examine_char function for each character
for c in s
examine_char(c)
endIn Julia, strings are UTF-8 encoded by default. Here’s a breakdown of the code:
We define a constant string
scontaining Thai characters.sizeof(s)gives the length of the string in bytes, similar tolen(s)in Go.We use
codeunits(s)to iterate over the raw bytes of the string and print their hexadecimal representation.length(s)counts the number of characters in the string, equivalent toutf8.RuneCountInString(s)in Go.We use
enumerate(s)to iterate over each character and its index in the string.nextind(s, 0, i)gives us the byte index of each character.We demonstrate another way to iterate over the string using the
iteratefunction, which is similar toutf8.DecodeRuneInStringin Go.We define an
examine_charfunction that checks for specific characters, similar to theexamineRunefunction in Go.Finally, we iterate over the string again, calling
examine_charfor each character.
In Julia, there’s no need to explicitly import packages for basic string operations. The language provides built-in support for Unicode strings and characters.
To run this program, save it as strings_and_chars.jl and use the Julia REPL or run it from the command line:
$ julia strings_and_chars.jlThis will output the character information, byte representation, and the results of character examination.