Skip to content

07 Strings

DodaTech 2 min read

In this tutorial, you will learn about Julia Strings Guide. We cover key concepts, practical examples, and best practices to help you master this topic.

Julia strings are UTF-8 encoded immutable byte sequences supporting interpolation with $, concatenation with * or string(), substring extraction, and regex pattern matching with r"..." literals and match/sub/replace functions.

Creating Strings

# Literal strings
s1 = "Hello, World!"
s2 = """Multi-line
string"""

# Interpolation
name = "Alice"
age = 30
greeting = "Hello, $name! You are $age years old."
calc = "2 + 2 = $(2 + 2)"

# Concatenation
s = "Hello" * " " * "World"
s = string("Hello", " ", "World")

String Operations

s = "Hello, Julia!"

# Length (characters, not bytes)
length(s)          # 14

# Indexing (byte positions, 1-based)
s[1]               # 'H' (Char, not string)
s[8]               # 'J'

# Substring
s[1:5]             # "Hello"
s[8:12]            # "Julia"

# Search
findfirst("Julia", s)  # 8:12 (UnitRange)
occursin("Julia", s)   # true
contains(s, "Julia")   # true

# Split and join
split("a,b,c", ",")    # ["a", "b", "c"]
join(["a", "b", "c"], "-")  # "a-b-c"

Regular Expressions

# Regex literal
email_pattern = r"[\w.]+@[\w.]+\.\w+"
phone_pattern = r"\d{3}-\d{3}-\d{4}"

# Matching
m = match(email_pattern, "Contact: user@example.com")
m.match          # "user@example.com"
m.captures       # [] (no capture groups)

# With capture groups
m = match(r"(\w+)@(\w+)\.(\w+)", "user@example.com")
m.captures       # ["user", "example", "com"]

# Replace
replace("Hello World", r"\w+" => uppercase)
# "HELLO WORLD"

# Test
occursin(r"\d+", "abc123")  # true

Useful Functions

# Case conversion
uppercase("hello")    # "HELLO"
lowercase("HELLO")    # "hello"
titlecase("hello world")  # "Hello World"

# Trimming
strip("  hello  ")    # "hello"
lstrip("  hello")     # "hello  "
rstrip("hello  ")     # "  hello"

# Padding
lpad("5", 3, "0")     # "005"
rpad("5", 3, "0")     # "500"

# Repeat
repeat("ha", 3)       # "hahaha"

Common Mistakes

1. Using + for concatenation

Julia uses * for string concatenation, not +. "Hello" + " " + "World" errors.

2. 1-based string indexing

s[0] errors. Strings use 1-based indexing like arrays. s[1] is the first character.

3. Byte vs character indexing

length(s) counts characters. sizeof(s) counts bytes. Non-ASCII characters use multiple bytes.

Practice Questions

1. How do you interpolate a variable into a string? Use $ prefix: "Hello, $name". For expressions: "Result: $(a + b)".

2. How do you concatenate strings? Use * operator: "Hello" * " " * "World", or string() function.

3. How do you test if a string matches a pattern? occursin(pattern, string) or contains(string, pattern) or match(pattern, string) !== nothing.

FAQ

{{< faq question="Are Julia strings mutable?" >}} No. Strings are immutable. Operations like replace return new strings. Use SubString for zero-copy substrings. {{< /faq >}}

{{< faq question="How do I convert a number to a string?" >}} Use string(42), repr(42), or sprintf("%.2f", 3.14) from Printf module. {{< /faq >}}

{{< faq question="How do I handle Unicode in strings?" >}} Julia strings are UTF-8. length counts characters (codepoints). Use eachindex(s) for valid byte positions. {{< /faq >}}

What's Next

Now learn about control flow in Julia.

Topic Description Link
Control Flow Conditionals and loops {{< ref "08-control-flow" >}}
Functions Defining functions {{< ref "09-functions" >}}

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro