Open In App

Get the first occurrence of the specified string pattern in Julia – findfirst() Method

The findfirst() is an inbuilt function in julia that is used to return the first occurrence of the specified pattern in a specified string.
 

Syntax: 



findfirst(Pattern::AbstractString, String::AbstractString)

Parameters: 

Returns: It returns a number in the format of First_number:Second_number. Where the first number is the first position of occurrence of the pattern in the specified string whereas the second number is the position in the string where the pattern ends. 
 



Example 1: 




# 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"))

Output: 
 

Example 2: 




# 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"))

Output: 
 

 


Article Tags :