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

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Spring Boot + Mockito simple application with 100% code coverage

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete