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 in
str = "Hello from Educative"
#substring to search for
substr = "Edu"
#check for a substring
println(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 substring substr in the string str.

  • Line 8: We will check if a substring substr contains in a string str using the function occursin(). We will pass the substring substr as a first parameter and string str as a second parameter to the function occursin().

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved