Go언어
strings 패키지
현준의코딩
2023. 10. 24. 21:22
strings 패키지에는 부분 문자열을 찾아 해당 문자열을 다른 문자열로 치환해주는 Replacer라는 타입이 있다.
아래 코드는 모든 문자열에 포함된 모든 #기호를 문자 o로 치환하는 예제이다.
package main
import (
"fmt"
"strings"
)
func main() {
broken := "G# r#cks!"
replacer := strings.NewReplacer("#", "o") // 모든 #을 o로 치환
fixed := replacer.Replace(broken) // Replace메서드를 호출해 치환할 문자열 전달.
fmt.Println(fixed)
// replacer : #을 o로 바꾸겠다는 문서.
// fixed : replacer라는 문서로 broken이라는 물건의 #을 o로 바꿔 fixed라는 이름으로 변경함!(update!)