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 

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Spring boot video streaming example-HTML5

Spring Boot + Mockito simple application with 100% code coverage

Spring Boot + OpenCSV Export Data to CSV Example

Custom Exception Handling in Quarkus REST API

Registration and Login with Spring Boot + Spring Security + Thymeleaf

DataTable-Pagination example with Spring boot, jQuery and ajax

Spring Webflux File Download - REST API Example

Node JS mini projects with source code - free download

ReactJS, Spring Boot JWT Authentication Example