Replace a substring with another string in Julia – replace() Method

The replace() is an inbuilt function in julia which is used to replace a word or character with the specified string or character.

Syntax:
replace(s::AbstractString, pattern=>Word; count::Integer)

Parameters:

  • s::AbstractString: Specified string.
  • pattern=>Word: Pattern is searched from the given sentence and then that pattern is replaced with the word.
  • count::Integer: It is a number which is the count of specified pattern available in the sentence.

Returns: It returns a new sentence with replaced words.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

# Julia program to illustrate 
# the use of String repleace() method
  
# Getting a new sentence with replaced words
println(replace("GFG is a CS portal.", "CS" => "Computer Science"))
println(replace("GeeksforGeeks is a CS portal.", "GeeksforGeeks" => "GFG"))

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Julia program to illustrate 
# the use of String repleace() method
  
# Getting a new sentence with replaced words
println(replace("GFG Geeks.", "GFG" => "GeeksforGeeks", count = 1))
println(replace("GFG Geeks GFG.", "GFG" => "GeeksforGeeks", count = 2))

chevron_right


Output:

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.