Introduction to strings

Einführung in Julia

James Fulton

Climate informatics researcher

Strings

# Strings are surrounded by " "
name = "Jane" # Cannot use 'Jane'


# Strings can be any length book = "It is a truth universally acknowledged, ..."
println(name)
println(book)
Jane

It is a truth universally acknowledged, ...
Einführung in Julia

Triple quotes

# Triple quotes 
poem = """Beware the Jabberwock, my son!

      The jaws that bite, the claws that catch!"""


println(poem)
Beware the Jabberwock, my son!

The jaws that bite, the claws that catch!
Einführung in Julia

Triple quotes

greeting = """ " Well hello there " """

println(greeting)
 " Well hello there " 
greeting = " " Well hello there " "


ERROR: syntax: ...
Einführung in Julia

Concatenating strings

name = "James"

greeting = "Well hello there, "


# Concatenate two strings
println(greeting*name)
Well hello there, James
Einführung in Julia

String interpolation

name = "James"


# Interpolate with $ symbol greeting = "Well hello there, $name"
println(greeting)
Well hello there, James
Einführung in Julia

String interpolation

x_int = 10


# Insert integer into string println("The value is $x_int")
The value is 10
x_float = 1.0


# Insert float into string println("The value is $x_float")
The value is 1.0
x_bool = true


# Insert boolean into string println("The value is $x_bool")
The value is true
x_char = 'A'


# Insert character into string println("The value is $x_char")
The value is A
Einführung in Julia

String interpolation

x = 10
y = 3


# Insert x*y into string println("The product of x and y is $(x*y)")
The product of x and y is 30
Einführung in Julia

String interpolation

x = 10
y = 3


# Insert x*y into string println("The product of x and y is \$(x*y)")
The product of x and y is $(x*y)
Einführung in Julia

Indexing strings

# Customer's seat
seat = "E5"


# Select character row = seat[1] # this returns 'E'

println(row)
println(typeof(row))
E

Char
Einführung in Julia

Indexing strings

# Customer's seat
seat = "E5"

# Select characters
row = seat[1]       # this returns 'E'
number = seat[2]    # this returns '5'


println("Your seat is in row $row, seat number $number.")
Your seat is in row E, seat number 5.
Einführung in Julia

Indexing strings

# Customer's seat
seat = "E5"

# Select characters
row = seat[1]       # this returns 'E'
number = seat[end]  # this returns '5'

println("Your seat is in row $row, seat number $number.")
Your seat is in row E, seat number 5.
Einführung in Julia

Indexing strings

# Customer's seat
seat = "E5"

# Select characters
row = seat[end-1]   # this returns 'E'
number = seat[end]  # this returns '5'

println("Your seat is in row $row, seat number $number.")
Your seat is in row E, seat number 5.
Einführung in Julia

Slicing strings




receipt = "08:30 - coffee - \$3.50" println(receipt)
08:30 - coffee - $3.50
Einführung in Julia

Slicing strings

# Index position:
#          12345...
receipt = "08:30 - coffee - \$3.50"


time = receipt[1:5] # Select first 5 characters
println(time)
08:30

Einführung in Julia

Slicing strings

# Index position from end:
#                            4321end
receipt = "08:30 - coffee - \$3.50"

time = receipt[1:5]         # Select first 5 characters

price = receipt[end-4:end] # Select last 5 characters
println(time)
println(price)
08:30

$3.50
Einführung in Julia

Let's practice!

Einführung in Julia

Preparing Video For Download...