Go 언어 문자열 조작

Beginner

This tutorial is from open-source community. Access the source code

소개

Go 언어의 strings 패키지는 문자열 관련 유용한 함수들을 많이 제공합니다. 이 랩은 이러한 함수들 중 일부에 대한 여러분의 이해도를 테스트하는 것을 목표로 합니다.

문자열 함수

strings 패키지에서 제공하는 다양한 문자열 함수의 출력을 인쇄하도록 아래 코드를 완성하십시오.

  • strings 패키지를 사용하여 랩을 완료하십시오.
  • fmt.Println 함수를 사용하여 출력을 인쇄하십시오.
  • 함수 이름이나 매개변수를 수정하지 마십시오.
$ go run string-functions.go
Contains: true
Count: 2
HasPrefix: true
HasSuffix: true
Index: 1
Join: a-b
Repeat: aaaaa
Replace: f00
Replace: f0o
Split: [a b c d e]
ToLower: test
ToUpper: TEST

전체 코드는 다음과 같습니다.

// The standard library's `strings` package provides many
// useful string-related functions. Here are some examples
// to give you a sense of the package.

package main

import (
	"fmt"
	s "strings"
)

// We alias `fmt.Println` to a shorter name as we'll use
// it a lot below.
var p = fmt.Println

func main() {

	// Here's a sample of the functions available in
	// `strings`. Since these are functions from the
	// package, not methods on the string object itself,
	// we need pass the string in question as the first
	// argument to the function. You can find more
	// functions in the [`strings`](https://pkg.go.dev/strings)
	// package docs.
	p("Contains:  ", s.Contains("test", "es"))
	p("Count:     ", s.Count("test", "t"))
	p("HasPrefix: ", s.HasPrefix("test", "te"))
	p("HasSuffix: ", s.HasSuffix("test", "st"))
	p("Index:     ", s.Index("test", "e"))
	p("Join:      ", s.Join([]string{"a", "b"}, "-"))
	p("Repeat:    ", s.Repeat("a", 5))
	p("Replace:   ", s.Replace("foo", "o", "0", -1))
	p("Replace:   ", s.Replace("foo", "o", "0", 1))
	p("Split:     ", s.Split("a-b-c-d-e", "-"))
	p("ToLower:   ", s.ToLower("TEST"))
	p("ToUpper:   ", s.ToUpper("test"))
}

요약

이 랩은 Go 언어의 strings 패키지에 대한 여러분의 이해도를 테스트합니다. 패키지에서 제공하는 다양한 함수를 사용하여 문자열을 조작할 수 있어야 합니다.