ZetCode

Julia String Functions Tutorial

last modified November 18, 2026

Julia ships with a rich set of functions for working with text. They let us measure a string, locate a piece of text inside it, split it into fields, substitute parts of it, trim whitespace, and change letter case. This tutorial introduces the functions used most often in everyday scripts.

Because Julia strings are UTF-8 encoded, positions are measured in bytes rather than characters; the examples below show how to handle that correctly. All examples were verified with Julia 1.13.4.

length, lastindex and sizeof

The length function counts characters, while sizeof counts the bytes used to store them. The lastindex function gives the byte index at which the last character ends, which is the largest index that can safely be used to slice into the string.

main.jl
s = "naïve café"
println(length(s))
println(lastindex(s))
println(sizeof(s))
println(s[1])
println(s[lastindex(s)])

The string has ten characters but occupies twelve bytes, because ï and é each need two bytes in UTF-8. Its last character begins at byte ten and ends at byte eleven, so lastindex(s) is eleven. Indexing with s[1] and s[lastindex(s)] returns the first and last characters. Using an arbitrary byte position in between can land in the middle of a character and raise an error.

$ julia main.jl
10
11
12
n
é

findfirst

The findfirst function searches for a substring or a character and reports where it occurs. For a substring it returns a UnitRange of byte indices; for a single character it returns one integer index. If nothing matches, it returns nothing.

main.jl
s = "Julia is fun"
println(findfirst("is", s))
println(findfirst('i', s))
println(findfirst("xyz", s))

The word "is" occupies positions seven and eight, so the result is 7:8, a range of indices. The first 'i' is at position four. The third search fails, and Julia prints nothing, the value that means "no result". Code that uses the result should check for nothing before indexing with it.

$ julia main.jl
7:8
4
nothing

findlast and findall

The findlast function returns the last place where a match occurs; findall returns every match as an array. Both work with a character or a substring.

main.jl
s = "banana"
println(findlast('a', s))
println(findall('a', s))
println(findall("na", s))

The character 'a' appears at positions two, four and six, so findlast reports six and findall returns all three indices. The substring "na" occurs twice: at 3:4 and 5:6. An array of ranges is printed in a compact form using the same 2:3 notation.

$ julia main.jl
6
[2, 4, 6]
UnitRange{Int64}[3:4, 5:6]

occursin

When we only need a yes/no answer, occursin is simpler than findfirst. It returns a Bool, that is, either true or false. Its first argument is the needle to look for and its second argument is the haystack to search.

main.jl
println(occursin("lib", "Julia library"))
println(occursin("py", "Julia library"))
println(occursin("i", "Julia"))

The first call finds "lib" inside "Julia library" and prints true. The second finds no "py", so it prints false. The third shows that a single character works as the needle as well. This function is a good fit for conditions in if statements and filters.

$ julia main.jl
true
false
true

split and join

The split function cuts a string into pieces at a delimiter and returns an array of SubString values, which are lightweight views into the original text. The join function does the reverse: it combines a collection of strings into one string with a separator between them.

main.jl
csv = "red,green,blue"
parts = split(csv, ",")
println(parts)
println(join(parts, " | "))
println(split("a  b   c"))

The comma-separated string becomes three fields, which join then assembles using " | " as the separator. With no delimiter argument, split breaks on runs of whitespace and drops empty fields, so the third line yields three words despite the extra spaces. The printed type SubString{String} tells us the pieces share storage with the original string.

$ julia main.jl
SubString{String}["red", "green", "blue"]
red | green | blue
SubString{String}["a", "b", "c"]

replace

The replace function returns a new string in which matching text has been substituted. The substitutions are written as pairs with the => operator: the old value on the left, the new one on the right. The original string is unchanged.

main.jl
s = "one fish, two fish"
println(replace(s, "fish" => "cat"))
println(replace(s, "fish" => "cat", count=1))
println(replace("aaa", 'a' => 'b'))

The first call replaces both occurrences of "fish". The count keyword limits the number of replacements, so the second call changes only the first one. The third call shows that characters work just like strings: every 'a' becomes a 'b'.

$ julia main.jl
one cat, two cat
one cat, two fish
bbb

strip, lstrip and rstrip

The strip function removes leading and trailing whitespace. Its variants lstrip and rstrip trim only the left or the right side. An optional second argument names the characters to remove instead of whitespace.

main.jl
s = "  padded text  "
println("[" * strip(s) * "]")
println("[" * lstrip(s) * "]")
println("[" * rstrip(s) * "]")
println(strip("xxhellox", ['x']))

The square brackets make the remaining spaces visible: strip removes both ends, lstrip only the beginning, and rstrip only the end. In the last line the array ['x'] tells strip to remove the letter x rather than whitespace. Trimming is a common first step when reading user input or fields from a file.

$ julia main.jl
[padded text]
[padded text  ]
[  padded text]
hello

Case Conversion

Julia provides uppercase and lowercase for changing the case of every letter, titlecase for capitalizing words, and the focused variants uppercasefirst and lowercasefirst for changing only the initial character.

main.jl
s = "Hello, World"
println(uppercase(s))
println(lowercase(s))
println(titlecase(s))
println(uppercasefirst("hello"))
println(lowercasefirst("HELLO"))

The first two calls change all letters at once and leave punctuation such as the comma untouched. titlecase capitalizes the beginning of each word. The last two calls affect only the first character, which is why "HELLO" becomes "hELLO" and not "hello". All of these functions return new strings.

$ julia main.jl
HELLO, WORLD
hello, world
Hello, World
Hello
hELLO

startswith and endswith

The startswith and endswith functions check a string's beginning and end. They are frequently used to recognize file extensions, URL schemes, or other fixed prefixes and suffixes.

main.jl
println(startswith("Julia.jl", "Julia"))
println(endswith("Julia.jl", ".jl"))
println(startswith("Julia.jl", "julia"))
println(endswith("julia.jl", ".txt"))

The first two checks succeed; the last two fail. Both functions are case-sensitive, so the lowercase "julia" is not a prefix of "Julia.jl". To ignore case, convert both strings with lowercase before comparing them.

$ julia main.jl
true
true
false
false

Putting the Functions Together

A typical task is to parse a line of text into fields. This example trims the input, splits it on semicolons, joins the fields with a different separator, and uppercases the first field.

main.jl
line = "  Alice;28;Bratislava  "
fields = split(strip(line), ";")
println(fields)
println(join(fields, " | "))
println(uppercase(first(fields)))

Without strip, the last field would contain two trailing spaces. The first function returns the first element of the array, here the substring "Alice", which uppercase converts to "ALICE". Composing small string functions like this is usually clearer than a long chain of manual index calculations.

$ julia main.jl
SubString{String}["Alice", "28", "Bratislava"]
Alice | 28 | Bratislava
ALICE

Source

Julia Strings documentation

Julia String Manual

In this article we have worked with string functions in Julia: measuring strings, finding text, testing for substrings, splitting and joining, replacing, trimming whitespace, changing case, and checking prefixes and suffixes. String basics are covered in Julia strings; pattern matching with regular expressions is covered in Julia regular expressions, and reading strings from files in Julia files.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Julia tutorials.