String Manipulation in Julia
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 strings = "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 stringi = 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 stringr = replace(s, "geek" => "champian") |

Julia
# replace the character with first letter i and last letter g with the word adventurousr = 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 stringss1 = "Geeks"s2 = "For"s3 = "Geeks" # concatenate stringsstring(s1, s2, s3) |

We can place any characters between the strings as well:
Julia
# concatenate strings with characters in betweenstring(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 stringscollect.([s1, s2, s3])# display length of the three stringslength.([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 typer = match(r"g[\w]*k", s)# display the matched charactershow(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 4r = eachmatch(r"[\w]{4, }", s)for i in r println("\"$(i.match)\" ")end |

Julia
# display characters with length equal to or greater than 4r = 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 stringr = strip("Geek ")# strip white spaces and the letter G in the stringr = strip("Geek ", ['G', ' ']) |

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

Julia
# split string on ', ' characterr = split("Geeks, for, Geeks", ", ") |

We can also split while removing characters which is represented in the following example:
Julia
# eliminating whitespaces while splitting the stringr = 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 betweenr = join(collect(1:7), ", ") |






Please Login to comment...