在 Julia 中用另一个字符串替换一个子字符串 – replace() 方法
replace()是 julia 中的一个内置函数,用于将单词或字符替换为指定的字符串或字符。
句法:
replace(s::AbstractString, pattern=>Word; count::Integer)
参数:
- s::AbstractString:指定字符串。
- pattern=>Word:从给定的句子中搜索模式,然后用单词替换该模式。
- count::Integer:它是一个数字,表示句子中可用的指定模式的计数。
返回:它返回一个带有替换词的新句子。示例 1:
Python
# Julia program to illustrate
# the use of String replace() 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"))
Python
# Julia program to illustrate
# the use of String replace() 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))
输出:
示例 2:
Python
# Julia program to illustrate
# the use of String replace() 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))
输出: