strings
แพ็กเกจ strings ใน Go จัดเตรียมฟังก์ชันสำหรับจัดการสตริง
การค้นหา
Contains
ตรวจสอบว่าสตริงมีสตริงย่อยหรือไม่
func Contains(s, substr string) boolตัวอย่าง
func main() {
fmt.Println(strings.Contains("hello world", "world"))
}ContainsAny
ตรวจสอบว่าสตริงมีอักขระใดอักขระหนึ่งในสตริงย่อยหรือไม่
func ContainsAny(s, chars string) boolตัวอย่าง
func main() {
fmt.Println(strings.ContainsAny("hello world", "abc"))
}ContainsRune
ตรวจสอบว่าสตริงมีอักขระ rune ที่ระบุหรือไม่
func ContainsRune(s string, r rune) boolตัวอย่าง
func main() {
fmt.Println(strings.ContainsRune("hello world", 'w'))
}HasPrefix
ตรวจสอบว่าสตริงขึ้นต้นด้วยสตริงย่อยหรือไม่
func HasPrefix(s, prefix string) boolตัวอย่าง
func main() {
fmt.Println(strings.HasPrefix("hello world", "hello"))
}HasSuffix
ตรวจสอบว่าสตริงลงท้ายด้วยสตริงย่อยหรือไม่
func HasSuffix(s, suffix string) boolตัวอย่าง
func main() {
fmt.Println(strings.HasSuffix("hello world", "world"))
}Index
ส่งคืนดัชนีแรกของสตริงย่อยในสตริง
func Index(s, substr string) intตัวอย่าง
func main() {
fmt.Println(strings.Index("hello world", "world"))
}IndexAny
ส่งคืนดัชนีแรกของอักขระใดอักขระหนึ่งในสตริงย่อย
func IndexAny(s, chars string) intตัวอย่าง
func main() {
fmt.Println(strings.IndexAny("hello world", "abc"))
}IndexByte
ส่งคืนดัชนีแรกของไบต์ที่ระบุ
func IndexByte(s string, c byte) intตัวอย่าง
func main() {
fmt.Println(strings.IndexByte("hello world", 'w'))
}IndexRune
ส่งคืนดัชนีแรกของ rune ที่ระบุ
func IndexRune(s string, r rune) intตัวอย่าง
func main() {
fmt.Println(strings.IndexRune("hello world", 'w'))
}LastIndex
ส่งคืนดัชนีสุดท้ายของสตริงย่อยในสตริง
func LastIndex(s, substr string) intตัวอย่าง
func main() {
fmt.Println(strings.LastIndex("hello world", "o"))
}LastIndexAny
ส่งคืนดัชนีสุดท้ายของอักขระใดอักขระหนึ่งในสตริงย่อย
func LastIndexAny(s, chars string) intตัวอย่าง
func main() {
fmt.Println(strings.LastIndexAny("hello world", "o"))
}LastIndexByte
ส่งคืนดัชนีสุดท้ายของไบต์ที่ระบุ
func LastIndexByte(s string, c byte) intตัวอย่าง
func main() {
fmt.Println(strings.LastIndexByte("hello world", 'o'))
}การนับ
Count
นับจำนวนครั้งที่สตริงย่อยปรากฏในสตริง
func Count(s, substr string) intตัวอย่าง
func main() {
fmt.Println(strings.Count("hello world", "o"))
}การเปรียบเทียบ
Compare
เปรียบเทียบสตริงสองสตริง
func Compare(a, b string) intตัวอย่าง
func main() {
fmt.Println(strings.Compare("a", "b"))
}Equal
ตรวจสอบว่าสตริงสองสตริงเท่ากันหรือไม่
func Equal(a, b string) boolตัวอย่าง
func main() {
fmt.Println(strings.EqualFold("Hello", "hello"))
}EqualFold
ตรวจสอบว่าสตริงสองสตริงเท่ากันหรือไม่ (ไม่แยกแยะตัวพิมพ์ใหญ่-เล็ก)
func EqualFold(a, b string) boolตัวอย่าง
func main() {
fmt.Println(strings.EqualFold("Hello", "hello"))
}การตัดต่อ
Repeat
ทำซ้ำสตริง n ครั้ง
func Repeat(s string, count int) stringตัวอย่าง
func main() {
fmt.Println(strings.Repeat("hello", 3))
}Replace
แทนที่สตริงย่อยเก่าด้วยสตริงใหม่ n ครั้ง หาก n เป็น -1 จะแทนที่ทั้งหมด
func Replace(s, old, new string, n int) stringตัวอย่าง
func main() {
fmt.Println(strings.Replace("hello world", "world", "go", -1))
}ReplaceAll
แทนที่สตริงย่อยเก่าด้วยสตริงใหม่ทั้งหมด
func ReplaceAll(s, old, new string) stringตัวอย่าง
func main() {
fmt.Println(strings.ReplaceAll("hello world", "world", "go"))
}ToUpper
แปลงสตริงเป็นตัวพิมพ์ใหญ่
func ToUpper(s string) stringตัวอย่าง
func main() {
fmt.Println(strings.ToUpper("hello"))
}ToLower
แปลงสตริงเป็นตัวพิมพ์เล็ก
func ToLower(s string) stringตัวอย่าง
func main() {
fmt.Println(strings.ToLower("HELLO"))
}ToTitle
แปลงอักขระแรกเป็นตัวพิมพ์ใหญ่
func ToTitle(s string) stringตัวอย่าง
func main() {
fmt.Println(strings.ToTitle("hello"))
}Trim
ตัดช่องว่างด้านหน้าและด้านหลังของสตริง
func Trim(s string, cutset string) stringตัวอย่าง
func main() {
fmt.Println(strings.Trim(" hello ", " "))
}TrimLeft
ตัดช่องว่างด้านหน้าของสตริง
func TrimLeft(s string, cutset string) stringตัวอย่าง
func main() {
fmt.Println(strings.TrimLeft(" hello ", " "))
}TrimRight
ตัดช่องว่างด้านหลังของสตริง
func TrimRight(s string, cutset string) stringตัวอย่าง
func main() {
fmt.Println(strings.TrimRight(" hello ", " "))
}TrimSpace
ตัดช่องว่างทั้งหมดด้านหน้าและด้านหลังของสตริง
func TrimSpace(s string) stringตัวอย่าง
func main() {
fmt.Println(strings.TrimSpace(" hello "))
}TrimPrefix
ตัดคำนำหน้าที่ระบุออกจากสตริง
func TrimPrefix(s, prefix string) stringตัวอย่าง
func main() {
fmt.Println(strings.TrimPrefix("hello world", "hello "))
}TrimSuffix
ตัดคำลงท้ายที่ระบุออกจากสตริง
func TrimSuffix(s, suffix string) stringตัวอย่าง
func main() {
fmt.Println(strings.TrimSuffix("hello world", " world"))
}การแยก
Split
แยกสตริงด้วยสตริงย่อย
func Split(s, sep string) []stringตัวอย่าง
func main() {
fmt.Println(strings.Split("hello world", " "))
}SplitAfter
แยกสตริงด้วยสตริงย่อย (รวมสตริงย่อย)
func SplitAfter(s, sep string) []stringตัวอย่าง
func main() {
fmt.Println(strings.SplitAfter("hello world", " "))
}SplitAfterN
แยกสตริงด้วยสตริงย่อย n ครั้ง (รวมสตริงย่อย)
func SplitAfterN(s, sep string, n int) []stringตัวอย่าง
func main() {
fmt.Println(strings.SplitAfterN("hello world foo bar", " ", 2))
}SplitN
แยกสตริงด้วยสตริงย่อย n ครั้ง
func SplitN(s, sep string, n int) []stringตัวอย่าง
func main() {
fmt.Println(strings.SplitN("hello world foo bar", " ", 2))
}การเชื่อมต่อ
Join
เชื่อมต่อสตริงด้วยตัวคั่น
func Join(elems []string, sep string) stringตัวอย่าง
func main() {
fmt.Println(strings.Join([]string{"hello", "world"}, " "))
}การจัดรูปแบบ
Title
แปลงคำแรกเป็นตัวพิมพ์ใหญ่
func Title(s string) stringตัวอย่าง
func main() {
fmt.Println(strings.Title("hello world"))
}Clone
โคลนสตริง
func Clone(s string) stringตัวอย่าง
func main() {
fmt.Println(strings.Clone("hello"))
}อื่นๆ
IsSpace
ตรวจสอบว่า rune เป็นช่องว่างหรือไม่
func IsSpace(r rune) boolตัวอย่าง
func main() {
fmt.Println(strings.IsSpace(' '))
}Map
แมปฟังก์ชันไปยังแต่ละอักขระในสตริง
func Map(mapping func(rune) rune, s string) stringตัวอย่าง
func main() {
fmt.Println(strings.Map(func(r rune) rune {
return r + 1
}, "hello"))
}