Julia String Functions
Julia's Base has a broad toolkit for working with text: changing case, searching, splitting, joining, replacing and matching regular expressions. These are plain functions, called as f(s) rather than s.f().
Case, trimming and padding
| Function | Example | Result |
|---|---|---|
uppercase, lowercase |
uppercase("abc") |
"ABC" |
titlecase |
titlecase("hello world") |
"Hello World" |
strip, lstrip, rstrip |
strip(" hi ") |
"hi" |
lpad, rpad |
lpad("7", 3, '0') |
"007" |
reverse |
reverse("abc") |
"cba" |
repeat |
repeat("ab", 3) |
"ababab" |
name = " grace hopper "
clean = strip(name)
println("[", clean, "]")
println(uppercase(clean), " / ", titlecase(clean))
println(lpad(42, 6, '.'), "|", rpad("left", 8), "|")
println(reverse("stressed"))
println(chop("file.txt", tail=4))[grace hopper]
GRACE HOPPER / Grace Hopper
....42|left |
desserts
file
Searching
text = "the quick brown fox jumps over the lazy dog"
println(occursin("fox", text))
println(startswith(text, "the"), " ", endswith(text, "cat"))
println(findfirst("the", text))
println(findlast("the", text))
println(findfirst('q', text))
println(count("o", text))
println(something(findfirst("cat", text), "not found"))true
true false
1:3
32:34
5
4
not found
findfirst returns a range of indices for a substring, an index for a character, or nothing when there is no match.
Splitting and joining
csv = "apple,banana,,cherry"
parts = split(csv, ",")
println(parts)
println(split(csv, ",", keepempty=false))
println(split(" many spaces here "))
println(join(["2025", "03", "14"], "-"))
println(join(1:5, ", ", " and "))
nums = parse.(Int, split("10 20 30"))
println(sum(nums))SubString{String}["apple", "banana", "", "cherry"]
SubString{String}["apple", "banana", "cherry"]
SubString{String}["many", "spaces", "here"]
2025-03-14
1, 2, 3, 4 and 5
60
split returns SubStrings, lightweight views into the original string. They behave like strings; call String(s) if you need a standalone copy. parse.(Int, ...) uses the dot syntax (see the Broadcasting lesson) to parse every piece.
Replacing
replace takes one or more old => new pairs:
s = "I like cats. Cats are great."
println(replace(s, "cats" => "dogs"))
println(replace(s, "cats" => "dogs", "Cats" => "Dogs"))
println(replace("a-b-c-d", "-" => "+", count=2))
println(replace("hello", 'l' => 'L'))I like dogs. Cats are great.
I like dogs. Dogs are great.
a+b+c-d
heLLo
Regular expressions
A regex literal is written r"...". Use occursin to test, match to get the first match with its capture groups, and eachmatch for all matches:
log = "2025-01-15 ERROR disk full; 2025-01-16 WARN cpu hot"
println(occursin(r"ERROR|FATAL", log))
m = match(r"(\d{4})-(\d{2})-(\d{2})", log)
println(m.match)
println(m.captures)
for d in eachmatch(r"\d{4}-\d{2}-(\d{2}) (\w+)", log)
println("day ", d[1], ": ", d[2])
end
println(replace(log, r"\d{4}-\d{2}-\d{2}" => "<date>"))true
2025-01-15
Union{Nothing, SubString{String}}["2025", "01", "15"]
day 15: ERROR
day 16: WARN
<date> ERROR disk full; <date> WARN cpu hot
Named groups work too: with r"(?<year>\d{4})", read the capture as m[:year].
Converting to and from strings
println(string(3.5), " ", string(10, base=2), " ", repr("quoted"))
println(parse(Float64, "2.75") * 2)
println(tryparse(Int, "12a"))
println(isnothing(tryparse(Int, "12a")))3.5 1010 "quoted"
5.5
nothing
true
parse throws an error for invalid input, while tryparse returns nothing, which is easier to handle in user-input code.
Test yourself
What does findfirst("cat", "dog") return?
- 0
- -1
- nothing
- an error
Which call returns "a+b+c"?
- replace("a-b-c", "-", "+")
- replace("a-b-c", "-" => "+")
- "a-b-c".replace("-", "+")
- sub("a-b-c", "-", "+")
What is the difference between parse and tryparse?
- None
tryparsereturnsnothingon invalid input instead of throwingtryparseonly works on integersparsereturns a string