10.2. String Functions#
Julia’s standard library provides a rich set of functions for manipulating strings. Let’s explore some of the most common ones.
10.2.1. Case Conversion#
You can easily convert the case of a string using uppercase, lowercase, and titlecase. These functions create and return a new string, leaving the original unchanged.
# Note that non-letter characters are unaffected.
s1 = uppercase("julia123 ")
s2 = lowercase("PYTHON")
println(s1 * s2)
JULIA123 python
The titlecase function capitalizes the first character of each word.
str_title = "the quick brown fox jumps over the lazy dog"
titlecase(str_title)
"The Quick Brown Fox Jumps Over The Lazy Dog"
10.2.2. Searching Within Strings#
To quickly check if a character is present in a string, you can use the ∈ operator (typed as \in followed by Tab).
# Let's check for a character in our title-cased string.
'T' ∈ titlecase(str_title)
true
To find the position of a pattern, use findfirst(pattern, string). It returns a range of indices where the pattern was found, or nothing if it wasn’t found.
str = "Hello, World! These are my words."
pattern = "wor"
# It's common to convert the string to lowercase for a case-insensitive search.
idx1 = findfirst(pattern, lowercase(str))
8:10
The function findnext(pattern, string, start_pos) finds the next occurrence of the pattern, beginning its search from the index start_pos.
# To find the *next* match, we start searching right after the end of the previous one.
idx2 = findnext(pattern, lowercase(str), idx1[end] + 1)
28:30
Similarly, findlast and findprev are available to search backwards from the end of the string.
10.2.3. Finding and Replacing Substrings#
Since strings are immutable, we can’t change them in place. Instead, the replace function creates a new string with the desired substitutions.
The syntax uses a Pair, like pattern => replacement.
# The original string `str` is not modified.
println("Original: ", str)
# A new string is returned with the replacement.
new_str = replace(str, ", World" => " there")
println("Replaced: ", new_str)
Original: Hello, World! These are my words.
Replaced: Hello there! These are my words.
10.2.4. Parsing Strings to Numbers#
A very common task in data processing is converting text into numbers. The parse(Type, string) function is essential for this. It attempts to interpret a string as a value of the specified numeric type.
# Parse a string into a 64-bit integer.
num = parse(Int, "1234")
println("$num is of type $(typeof(num))")
1234 is of type Int64
# You can specify a different base for number systems like binary (base 2).
parse(Int, "10110", base=2)
22
# This also works for floating-point numbers, including scientific notation.
parse(Float64, "1.2e-3")
0.0012
This function is especially powerful when you need to read numerical data from a file, which often gives you an array of strings.
# An array of strings containing numbers.
string_numbers = ["123.4", "4590.12", "0.456"]
# The dot syntax `parse.(...)` broadcasts the function over the entire array,
# converting each string to a Float64 and returning a new array of numbers.
parse.(Float64, string_numbers)
3-element Vector{Float64}:
123.4
4590.12
0.456