3  String

3.1 Create String

Enclose your String in ” ” or ““” “““!

s1 = "I am a string."
#> "I am a string."
typeof(s1)
#> String
s2 = """I am also a string. """
#> "I am also a string. "
typeof(s2)
#> String

Multi-line string should enclosed with triple quotes. The indentation will be ignored by Julia to improve readablility.

s = """
    This is a big multiline string with a nested "quotation".
    As you can see.
    It is still a String to Julia.
    """
#> "This is a big multiline string with a nested \"quotation\".\nAs you can see.\nIt is still a String to Julia.\n"
print(s)
#> This is a big multiline string with a nested "quotation".
#> As you can see.
#> It is still a String to Julia.

Single quote is for Char

typeof('a')
#> Char

3.2 String Interpolation

Similar to shell

name = "Joe"
num_fingers = 10
num_toes = 10

Use $ to refer to variable.

println("Hello, my name is $name.")
#> Hello, my name is Joe.
println("I have $num_fingers fingers and $num_toes toes.")
#> I have 10 fingers and 10 toes.

Run expression in $(command)

println("That is $(num_fingers + num_toes) digits in all!!")
#> That is 20 digits in all!!

3.3 Concatenate String

s3 = "How many cats ";
s4 = "is too many cats?";
😺 = 10

string() converts non-string inputs to strings.

string(s3, s4)
#> "How many cats is too many cats?"

We can also use * for concatenation!

s3 * s4
#> "How many cats is too many cats?"

join() is better. It allows specifying delim and last separactor

fruit = ["apples", "bananas", "pineapples"]
#> 3-element Vector{String}:
#>  "apples"
#>  "bananas"
#>  "pineapples"

join(fruit, ", ", " and ")
#> "apples, bananas and pineapples"

3.4 Vectorize String

Let’s greet some people

h = "Hello"
#> "Hello"
people = ["Marty", "Johny"]
#> 2-element Vector{String}:
#>  "Marty"
#>  "Johny"
typeof(people)
#> Vector{String} (alias for Array{String, 1})
string.(h, " ", people)
#> 2-element Vector{String}:
#>  "Hello Marty"
#>  "Hello Johny"
h .* " " .* people
#> 2-element Vector{String}:
#>  "Hello Marty"
#>  "Hello Johny"

Now write function hello() with 2 methods

hello(people::AbstractString) = "Hello" * " " * people
#> hello (generic function with 1 method)
hello(people::AbstractArray) = "Hello" .* " " .* people
#> hello (generic function with 2 methods)
methods(hello)
#> # 2 methods for generic function "hello":
#> [1] hello(people::AbstractString) in Main at none:3
#> [2] hello(people::AbstractArray) in Main at none:3
hello("A")
#> "Hello A"
students = ["Harry", "Ron"]
#> 2-element Vector{String}:
#>  "Harry"
#>  "Ron"
hello(students)
#> 2-element Vector{String}:
#>  "Hello Harry"
#>  "Hello Ron"

3.5 String Manipulation

julia_string = "Julia is an amazing open source programming language"
#> "Julia is an amazing open source programming language"

Regex define with r“text”

r"sometext"
#> r"sometext"
typeof(r"sometext")
#> Regex

3.5.1 Conditional Testing

substring of the first argument

contains(julia_string, "Julia")
#> true
# Regex
contains(julia_string, r"J.+e$")
#> true

Starts With of the first argument

startswith(julia_string, "Julia")
#> true

Ends With of the first argument

endswith(julia_string, "Julia")
#> false

3.5.2 Change Cases

lowercase(julia_string)
#> "julia is an amazing open source programming language"
uppercase(julia_string)
#> "JULIA IS AN AMAZING OPEN SOURCE PROGRAMMING LANGUAGE"
titlecase(julia_string)
#> "Julia Is An Amazing Open Source Programming Language"

3.5.3 Replace & Split

replace("R is a programming language.", "R" => "Julia")
#> "Julia is a programming language."

count args

replace("R user comes from useR.", "R" => "Julia")
#> "Julia user comes from useJulia."
replace("R user comes from useR.", "R" => "Julia", count=1)
#> "Julia user comes from useR."
split("a b c d")
#> 4-element Vector{SubString{String}}:
#>  "a"
#>  "b"
#>  "c"
#>  "d"
split("a, b, c, d", r"\s*,\s*")
#> 4-element Vector{SubString{String}}:
#>  "a"
#>  "b"
#>  "c"
#>  "d"

3.6 String Conversion

parse(Int64, "123")
#> 123
parse(Int64, "a") # Error

Silently parse to nothing

tryparse(Int64, "A very non-numeric string")

3.7 Exercise

Create a string that says “hi” 3 times, first with repeat and then with the exponentiation operator, which can call * under the hood. Assign it the variable hi below.

repeat("hi", 3)
#> "hihihi"
"hi"^3
#> "hihihi"

Declare two variables

a = 3
b = 4

and use them to create two strings:

c = string(a) * " + " * string(b)
#> "3 + 4"
d = string(a + b)
#> "7"