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)
end

In Julia, strings are UTF-8 encoded by default. Here’s a breakdown of the code:

  1. We define a constant string s containing Thai characters.

  2. sizeof(s) gives the length of the string in bytes, similar to len(s) in Go.

  3. We use codeunits(s) to iterate over the raw bytes of the string and print their hexadecimal representation.

  4. length(s) counts the number of characters in the string, equivalent to utf8.RuneCountInString(s) in Go.

  5. 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.

  6. We demonstrate another way to iterate over the string using the iterate function, which is similar to utf8.DecodeRuneInString in Go.

  7. We define an examine_char function that checks for specific characters, similar to the examineRune function in Go.

  8. Finally, we iterate over the string again, calling examine_char for 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.jl

This will output the character information, byte representation, and the results of character examination.