Skip to content
Related Articles

Related Articles

Improve Article

String Manipulation in Julia

  • Last Updated : 25 Aug, 2020

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: 


#  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:


#  find the index of occurance 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:


#  find the start and end indices of occurance of the word 'geek'
r = findfirst("geek", s)


We can also search for a word by specifying its first and last letters:


#  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.


# replace the word geek with champian in the string
r = replace(s, "geek" => "champian")


# 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.


# 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:


# concatenate strings with characters in between
string(s1, ", ", s2, "- ", s3)

Using ‘*’ operator for direct string concatenation:


# 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:


# 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:


# 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.


# display characters with length 
# equal to or greater than 4
r = eachmatch(r"[\w]{4, }", s)
for i in r println("\"$(i.match)\" ")


# 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:


# 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:


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


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

We can also split while removing characters which is represented in the following example:


# 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.


# joining characters or strings with ', ' in between
r = join(collect(1:7), ", ")

My Personal Notes arrow_drop_up
Recommended Articles
Page :