Contains Function
Lesson 14 of 19 in Coddy's Functions and Pointers in Golang course.
Another built-in function that we can use in Go is Contains, which checks if a given string contains a character or a substring.
An example of how this function works:
str := "Go"
c := "o"
b := strings.Contains(str, c)
fmt.Println(b)The function is called Contains, and it's within the strings package. This function takes two string values: the first value str is the string we will check, and c is the value we will look for in the str string. It returns a boolean value: true if c exists in the string str, and false if c is not in the string str.
In this example, we will get true because "o" exists in the string "Go".
Challenge
EasyLet's build our Contains function! 😃
Write a function called contains that takes two string values and returns a boolean value (true or false).
The first value is the string, and the second is the character that we're going to check for existence in the string value.
In our example, we will check for a single character.
Example:
Input:
"Coddy"
"y"Output:
trueTry it yourself
// Your Code HereAll lessons in Functions and Pointers in Golang
1Introduction
Introduction4Rebuild Go Strings Functions
Strings in GolangJoin FunctionContains FunctionIndex FunctionRecap Challenge #1