Go Language, Program to check if a string contains substring

Go language provides a powerful strings package that holds different types of methods to manipulate UTF-8 encoded strings.

How to check if a string contains a substring in Go Language

String Contains() function checks whether the substring is within the string. The Contains() function accepts two arguments and returns the boolean value, either true or false.
package main

import (
"fmt"
"strings"
)

func main() {
str := "HelloWorld Example"
fmt.Println(strings.Contains(str, "World"))
fmt.Println(strings.Contains(str, "exam"))
fmt.Println(strings.Contains(str, "Hell"))
}

Output:




How to check if a string contains any chars in the provided string.

package main

import (
"fmt"
"strings"
)

func main() {
str := "HelloWorld Example"
fmt.Println(strings.ContainsAny(str, "moon"))
fmt.Println(strings.ContainsAny(str, "Fun"))
fmt.Println(strings.ContainsAny(str, "exam"))
}

Output:


You can also check out golang.org/pkg/strings/#ContainsAny 

Comments

Popular posts from this blog

Spring Boot OpenAI Integration: Step-by-Step Guide

Orchestration-Based Saga Architecture and Spring Boot Microservices Implementation Guide

Spring Boot 3 + Angular 15 + Material - Full Stack CRUD Application Example