English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang 기본 강의

Golang 제어문

Golang 함수 & 메서드

Golang 구조체

Golang 슬라이스 & 배열

Golang 문자열(String)

Golang 포인터

Golang 인터페이스

Golang 병목

Golang 예외(Error)

Golang 다른杂项

Go 문자열이 특정 문자를 포함하는지�断

Go 문자열에서는 주어진 함수를 사용하여 문자열에 존재하는 주어진 문자를 확인할 수 있습니다. 이 함수들은 문자열 패키지에서 정의되어 있으며, 이 함수에 접근하기 위해 문자열 패키지를 프로그램에 가져오아야 합니다:

1.Contains:이 함수는 주어진 문자열에 주어진 문자가 존재하는지 확인하는 데 사용됩니다. 문자가 주어진 문자열에 존재하면 true를 반환하고, 그렇지 않으면 false를 반환합니다.

문법:

func Contains(str, chstr string) bool

여기서는str은 원래 문자열이며}}chstr은 확인하려는 문자열입니다. 이 개념에 대해 예제를 통해 논의해 보겠습니다:

//문자열에 존재하는지 확인
//지정된 문자열
package main
import (
    "fmt"
    "strings"
)
func main() {
    //문자열 생성 및 초기화
    str1 := "Welcome to w"3codebox for w3codebox "
    str2 := "Here! we learn about go strings"
    fmt.Println("원래 문자열"
    fmt.Println("String" 1: "", str1)
    fmt.Println("String" 2: "", str2)
    //문자열이 존재하는지 확인
    //Contains() 함수 사용
    res1 := strings.Contains(str"1, "w"3codebox")
    res2 := strings.Contains(str"2, "GFG"
    //결과 표시
    fmt.Println("\nResult" 1: "", res1)
    fmt.Println("Result" 2: "", res2)
}

출력:

원래 문자열
String 1: "Welcome to w"3codebox for w3codebox
String 2: "Here! we learn about go strings"
결과 1: true
결과 2:  false

 
2. ContainsAny:이 함수는 주어진 문자열(str)에 charstr에 포함된 어떤 Unicode 문자가 있는지 확인합니다. 주어진 문자열(str)에 charstr에 포함된 어떤 Unicode 문자가 있으면 이 메서드는 true를 반환하고, 그렇지 않으면 false를 반환합니다.

문법:

func ContainsAny(str, charstr string) bool

여기서는str 은 원래 문자열입니다.charstr chars의 Unicode 문자입니다. 이 개념에 대해 예제를 통해 논의해 보겠습니다:

//지정된 문자열이 존재하거나 존재하지 않습니다.
package main
import (
    "fmt"
    "strings"
)
func main() {
    //문자열 생성 및 초기화
    str1 := "Welcome to Geeks for Geeks"
    str2 := "Here! we learn about go strings"
    //문자열이 존재하는지 확인
    //ContainsAny() 함수 사용
    res1 := strings.ContainsAny(str"1, "Geeks"
    res2 := strings.ContainsAny(str"2, "GFG"
    res3 := strings.ContainsAny("w"3codebox", "G & f"
    res4 := strings.ContainsAny("w"3codebox", "u | e"
    res5 := strings.ContainsAny("  ", "  ")
    res6 := strings.ContainsAny("w"3codebox", "  ")
    //결과 표시
    fmt.Println("\nResult" 1: "", res1)
    fmt.Println("Result" 2: "", res2)
    fmt.Println("Result" 3: "", res3)
    fmt.Println("Result" 4: "", res4)
    fmt.Println("Result" 5: "", res5)
    fmt.Println("Result" 6: "", res6)
}

출력:

결과 1: true
결과 2:  false
결과 3:  false
결과 4:  false
결과 5: true
결과 6:  false