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
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
i = findfirst(isequal( 'd' ), s)
|
We can place an entire word which returns the start and end index of that word in the string:
We can also search for a word by specifying its first and last letters:
Julia
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
r = replace(s, "geek" = > "champian" )
|
Julia
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
s1 = "Geeks"
s2 = "For"
s3 = "Geeks"
string(s1, s2, s3)
|
We can place any characters between the strings as well:
Julia
string(s1, ", " , s2, "- " , s3)
|
Using ‘*’ operator for direct string concatenation:
Getting size of Strings
collect() and length() functions can be used to get all the characters and lengths of multiple strings respectively:
Julia
collect.([s1, s2, s3])
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
r = match(r "g[\w]*k" , s)
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
r = eachmatch(r "[\w]{4, }" , s)
for i in r println( "\"$(i.match)\" " )
end
|
Julia
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
r = strip( "Geek " )
r = strip( "Geek " , [ 'G' , ' ' ])
|
split() function can be used to split a string on a specific character:
Julia
r = split( "Geeks, for, Geeks" , ', ' )
|
Julia
r = split( "Geeks, for, Geeks" , ", " )
|
We can also split while removing characters which is represented in the following example:
Julia
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
r = join(collect( 1 : 7 ), ", " )
|
Share your thoughts in the comments
Please Login to comment...