How to check for a substring using Julia
A string is a sequence of characters in Julia. The strings can be defined in double quotes "". The individual characters can be accessed in a string using the index.
We can check for a substring in a string using the function occursin().
Problem Statement
Input:
String = "Hello from Educative"
Substring = "Edu"
Output:
true
Syntax
occursin(substring, string)
Parameters
This function takes two parameters.
substring: We will pass the first parameter as a substring to search for.string: We will pass the second parameter as a string to search in.
Return values
The occursin() function returns a boolean value. It will return true if a substring is found in the string else it will return false.
Example
Let us take a look at an example of occursin() function.
# string to search instr = "Hello from Educative"#substring to search forsubstr = "Edu"#check for a substringprintln(occursin(substr, str))
Explanation
Line 2: We declare and initialize a variable
str. We will use this string to search for a substring in it.Line 5: We declare and initialize a variable
substr. We will search for this substringsubstrin the stringstr.Line 8: We will check if a substring
substrcontains in a stringstrusing the functionoccursin(). We will pass the substringsubstras a first parameter and stringstras a second parameter to the functionoccursin().
Free Resources