String Manipulation in Julia
Last Updated :
04 Apr, 2022
String manipulation is the process of analyzing and handling of strings to achieve the desired result by manipulating or changing the data present in the string. Julia offers various string operations we can apply to the strings to achieve that. We will discuss such operations further in detail.
First, we create a string to manipulate:
Julia
# Create a string
s = "Everybody has an interesting geek in them"

Searching elements in a String
To search for one or more elements in a string we can use findfirst() function which returns the first index of the character we place as shown below:
Julia
# find the index of occurrence of the letter 'd' in the string
i = findfirst(isequal('d'), s)

We can place an entire word which returns the start and end index of that word in the string:
Julia
# find the start and end indices of occurrence of the word 'geek'
r = findfirst("geek", s)

We can also search for a word by specifying its first and last letters:
Julia
# find the indices of characters with first letter 'i' and last letter 'g'
r = findfirst(r"i[\w]*g", s)

Replacing elements of a String
To replace elements or characters in the string with other elements replace() function can be used by specifying the characters to be replaced in the same way.
Julia
# replace the word geek with champian in the string
r = replace(s, "geek" => "champian")

Julia
# replace the character with first letter i and last letter g with the word adventurous
r = replace(s, r"i[\w]*g" => "adventurous")

Concatenation of Strings
Concatenation is an operation of linking things together in a series. It is one of the most useful string operations and Julia offers a very simple way to achieve it.
Julia
# create three strings
s1 = "Geeks"
s2 = "For"
s3 = "Geeks"
# concatenate strings
string(s1, s2, s3)

We can place any characters between the strings as well:
Julia
# concatenate strings with characters in between
string(s1, ", ", s2, "- ", s3)

Using '*' operator for direct string concatenation:
Julia
# concatenate three strings with *
s1*", "*s2*", "*s3

Getting size of Strings
collect() and length() functions can be used to get all the characters and lengths of multiple strings respectively:
Julia
# display all characters in all of the three strings
collect.([s1, s2, s3])
# display length of the three strings
length.([s1, s2, s3])

Performing Match operation
Julia allows us to use match() function to scan the string left to right for the first match (specified starting index optional) which returns RegexMatch types:
Julia
# display element matching character
# with first letter g and last letter k
# with Regekmatch type
r = match(r"g[\w]*k", s)
# display the matched character
show(r.match); println()

We can use eachmatch() function to run iterations over the string. The following examples show the implementation of matching elements with specified lengths.
Julia
# display characters with length
# equal to or greater than 4
r = eachmatch(r"[\w]{4, }", s)
for i in r println("\"$(i.match)\" ")
end

Julia
# display characters with length equal to or greater than 4
r = collect(m.match for m = eachmatch(r"[\w]{3, }", s))

Strip, Split and Join operations
strip() function is used to eliminate certain characters in a string. If no characters are mentioned which we want to eliminate in the function, strip() function eliminates the white spaces in the string. We can mention the characters we want to eliminate in the string by placing them as second arguments in the strip() function which are represented in the following example:
Julia
# strip white spaces in the string
r = strip("Geek ")
# strip white spaces and the letter G in the string
r = strip("Geek ", ['G', ' '])

split() function can be used to split a string on a specific character:
Julia
# split string on ', ' character
r = split("Geeks, for, Geeks", ', ')

Julia
# split string on ', ' character
r = split("Geeks, for, Geeks", ", ")

We can also split while removing characters which is represented in the following example:
Julia
# eliminating whitespaces while splitting the string
r = split("Geeks, for, Geeks", [', ', ' '],
limit = 0, keepempty = false)

join() function is the opposite of split, with which we can join strings with a specific character in between.
Julia
# joining characters or strings with ', ' in between
r = join(collect(1:7), ", ")

Similar Reads
String Manipulation in R
String manipulation is a process of handling and analyzing strings. It involves various operations of modification and parsing of strings to use and change its data. R offers a series of in-built functions to manipulate a string. In this article, we will study different functions concerned with the
4 min read
Manipulating matrices in Julia
Matrices in Julia are the heterogeneous type of containers and hence, they can hold elements of any data type. It is not mandatory to define the data type of a matrix before assigning the elements to the matrix. Julia automatically decides the data type of the matrix by analyzing the values assigned
4 min read
Sorting of Strings in Julia
Sorting of strings in Julia means that now we are sorting the characters of the string. In Julia, this can easily be achieved with the basic logic of getting each character of the string into a separate array and sorting the array. Lastly joining the array together to get the sorted array. Major tas
4 min read
Strings in Julia
Strings in Julia are a set of characters or a single character that is enclosed within double quotes(" "). It is a finite sequence of characters. Julia allows extracting elements of a string to form multiple substrings with the use of square brackets([ ]). Creating a String Strings in Julia can be c
6 min read
Pattern Matching in Julia
Pattern matching is the process of checking whether a specific sequence of characters/tokens/data exists among the given data. Regular programming languages make use of regular expressions (regex) for pattern matching. In Julia to match given string with a given pattern following pre-defined functi
3 min read
Visualisation in Julia
Data visualization is the process of representing the available data diagrammatically. There are packages that can be installed to visualize the data in languages like Python and Julia. Some of the reasons that make the visualization of data important are listed below: Larger data can be analyzed ea
7 min read
Storing Output on a File in Julia
Julia provides a vast library to store and save the output in multiple file formats. We can store the output in various forms such as CSV(comma-separated value) or in Excel or just simply a text file. Storing Data on Text FileUsing open() function In order to store the data in a text file, we need t
4 min read
Serialization in Julia
Like other programming languages, Julia also provides support for Serialization and De-serialization. The process of conversion of an object into byte streams (IO buffers) for the purpose of storing it into memory, file, or database is termed as Serialization. It is performed to save the object stat
2 min read
Operator Overloading in Julia
Operator overloading in Julia refers to the ability to define custom behavior for built-in operators such as +, -, *, /, etc. when applied to objects of custom types. This allows users to create intuitive and concise code that works with custom data types as if they were built-in. In Julia, operator
5 min read
Get repetition of strings in Julia - ^ operator
The ^ is an operator in julia which is used to repeat the specified string with the specified number of times. Syntax: ^(s::Union{AbstractString, AbstractChar}, n::Integer) Parameters:  s::Union{AbstractString, AbstractChar}: Specified strings or charactersn::Integer: Specified integer Returns: I
1 min read