Julia Strings
A string is a sequence of characters written between double quotes. A single character, written between single quotes, has its own type: Char.
s = "Hello, Julia"
c = 'J'
println(s, " is a ", typeof(s))
println(c, " is a ", typeof(c))
println(length(s))
println("Line one\nLine two\tTabbed")
println("She said \"hi\"")Hello, Julia is a String
J is a Char
12
Line one
Line two Tabbed
She said "hi"
Strings are immutable: you can't change a character in place, you build a new string instead.
Concatenation and interpolation
Use * (not +) to join strings, ^ to repeat them, and $ to insert values:
first = "Ada"
last = "Lovelace"
full = first * " " * last
println(full)
println("-"^12)
println(string("Year: ", 1843, ", pages: ", 66))
born = 1815
println("$first was born in $born.")
println("In 1843 she was $(1843 - born) years old.")
println("A literal dollar sign: \$5")Ada Lovelace
------------
Year: 1843, pages: 66
Ada was born in 1815.
In 1843 she was 28 years old.
A literal dollar sign: $5
* was chosen because string concatenation, like multiplication, is not commutative. $name inserts a variable; $(expr) inserts the value of any expression.
Indexing and slicing
Strings are indexed from 1 with square brackets. begin and end refer to the first and last index. Indexing returns a Char; a range returns a String:
word = "Programming"
println(word[1])
println(word[end])
println(word[1:7])
println(word[end-3:end])
println(typeof(word[1]), " ", typeof(word[1:2]))
println(first(word, 3), " ", last(word, 3))P
g
Program
ming
Char String
Pro ing
Unicode and byte indices
Julia strings are stored as UTF-8. Characters outside ASCII take more than one byte, so string indices are byte positions, and not every integer is a valid index:
s = "café ☕"
println(length(s), " characters, ", sizeof(s), " bytes")
println(collect(eachindex(s)))
try
s[5]
catch e
println(sprint(showerror, e))
end
for ch in s
print(ch, "|")
end
println()6 characters, 9 bytes
[1, 2, 3, 4, 6, 7]
StringIndexError: invalid index [5], valid nearby indices [4]=>'é', [6]=>' '
c|a|f|é| |☕|
Iterating with for ch in s always gives whole characters, so prefer it (or eachindex, nextind, first/last) over manual index arithmetic when text might contain non-ASCII characters.
Multi-line and raw strings
Triple-quoted strings can span several lines; leading indentation shared by all lines is removed. A raw"..." string does no interpolation or escaping, which is handy for Windows paths and regular expressions.
poem = """
Roses are red,
violets are blue.
"""
print(poem)
path = raw"C:\Users\ada\$notes.txt"
println(path)Roses are red,
violets are blue.
C:\Users\ada\$notes.txt
Characters are numbers underneath
A Char is a Unicode code point, so you can compare characters, do arithmetic with them and convert to and from integers:
println(Int('A'), " ", Char(97))
println('a' < 'b', " ", 'c' - 'a')
println('A' + 2)
println([Char(c) for c in 72:76])
println(isdigit('7'), " ", isletter('x'), " ", uppercase('q'))65 a
true 2
C
['H', 'I', 'J', 'K', 'L']
true true Q
Test yourself
Which operator concatenates two strings in Julia?
- +
- *
- &
- ..
What is "Julia"[end]?
- "a"
- 'a'
- 5
- an error
How do you insert the value of x + 1 into a string?
- "{x + 1}"
- "$(x + 1)"
- "#{x + 1}"
- "%(x + 1)"