获取 Julia 中指定字符串模式的第一次出现 – findfirst() 方法
findfirst()是 julia 中的一个内置函数,用于返回指定字符串中指定模式的第一次出现。
句法:
findfirst(Pattern::AbstractString, String::AbstractString)
参数:
- 模式:指定要搜索的模式
- 字符串:指定字符串
返回:它以 First_number:Second_number 的格式返回一个数字。其中第一个数字是指定字符串中模式出现的第一个位置,而第二个数字是模式结束的字符串中的位置。
示例 1:
Python
# Julia program to illustrate
# the use of String findfirst() method
# Here the pattern is "g" and String is
# "geeks"
Println(findfirst("g", "geeks"))
# Here the pattern is "eek" and String is
# "geeks"
Println(findfirst("eek", "geeks"))
# Here the pattern is "ks" and String is
# "geeks"
Println(findfirst("ks", "geeks"))
# Here the pattern is "geeks" and String is
# "geeksforgeeks"
Println(findfirst("geeks", "geeksforgeeks"))
Python
# Julia program to illustrate
# the use of String findfirst() method
# Here the pattern is "4" and String is
# "2468"
Println(findfirst("4", "2468"))
# Here the pattern is "234" and String is
# "123456"
Println(findfirst("234", "123456"))
# Here the pattern is "45" and String is
# "123456"
Println(findfirst("45", "123456"))
输出:
示例 2:
Python
# Julia program to illustrate
# the use of String findfirst() method
# Here the pattern is "4" and String is
# "2468"
Println(findfirst("4", "2468"))
# Here the pattern is "234" and String is
# "123456"
Println(findfirst("234", "123456"))
# Here the pattern is "45" and String is
# "123456"
Println(findfirst("45", "123456"))
输出: