Open In App

String Manipulation in Julia

Last Updated : 04 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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"


manipulation-01

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)


manipulation-02

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)


manipulation-03

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), ", ")




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads