獲取字符串長(zhǎng)度
已知字符串在Go語(yǔ)言中的本質(zhì)是一個(gè)切片,如果想獲得切片的長(zhǎng)度,可以通過(guò)len()函數(shù)獲取
所以可以通過(guò)len() 函數(shù)來(lái)獲取字符串的長(zhǎng)度
案例
package main
import "fmt"
func main() {
str := "abcde"
length := len(str)
fmt.Println("length = ",length) // length = 5
}
- 注意點(diǎn)
- len()函數(shù)在獲取字符串的時(shí)候 , 獲取的是字節(jié)數(shù)
- 在Go語(yǔ)言中 ,中文 是按照UTF-8編碼的, 所以一個(gè)中文占用3個(gè)字節(jié)
package main
import "fmt"
func main() {
str := "好"
length := len(str)
fmt.Println(length) // length = 3
}
- 如果像獲取中文的個(gè)數(shù),而不是字節(jié)數(shù),那么需要講字符轉(zhuǎn)換為rune類(lèi)型的切片才行
- 案例
package main
import "fmt"
func main() {
str := "好好學(xué)習(xí)"
length := len(str)
fmt.Println("length = ",length) // length = 12
var str2 []rune = []rune(str) // 轉(zhuǎn)換為rune 類(lèi)型的切片 強(qiáng)制轉(zhuǎn)換--> 轉(zhuǎn)換的數(shù)據(jù)類(lèi)型(要轉(zhuǎn)換的數(shù)據(jù))
length2 := len(str2)
fmt.Println("length2 = ",length2) //length = 4
}
字符串的查找
- 查找字符串出現(xiàn)的位置
從左至右查找的函數(shù)
- 功能:
- 超找指定字符在字符串中的位置
- 會(huì)從左至右的超找, 一旦查找到指定字符就不再查找,并返回該字符的位置, 如果沒(méi)有查找到返回-1
1. func IndexByte(s string, c byte) int
-
注意點(diǎn)
只能超找字符不能查找中文
package main
import (
"fmt"
"strings"
)
func main() {
str := "www.reibang.com"
index := strings.IndexByte(str,'c') // 查找字符
fmt.Println("index = ",index) // index = 12
}
2. func IndexRune(s string, r rune) int
- 注意點(diǎn)
可以超找字符也可以查找中文
查找中文時(shí) 一個(gè)中文占用3 個(gè)索引位置
package main
import (
"fmt"
"strings"
)
func main() {
str := "www.簡(jiǎn)書(shū).com"
index := strings.IndexRune(str,'簡(jiǎn)')
index2 := strings.IndexRune(str,'w')
fmt.Println("index = ",index) // index = 4
fmt.Println("index = ",index2) // index = 0
}
3.func IndexAny(s, chars string) int
- 注意點(diǎn)
超找時(shí)會(huì)將指定查找的字符拆分開(kāi)來(lái)查找, 查找完返回其中位置最靠前的字符的位置 ----> (支持中文和字符)
package main
import (
"fmt"
"strings"
)
func main() {
str := "www.reibang.com"
index := strings.IndexAny(str,"wjc")
fmt.Println("index = ",index) // index = 0
}
4.func Index(s, sep string) int
- 注意點(diǎn)
- 超找時(shí)會(huì)將指定查找的字符看作一個(gè)整體來(lái)查找, 查找完返回其位置** ----> (支持中文和字符)
package main
import (
"fmt"
"strings"
)
func main() {
str := "www.簡(jiǎn)書(shū).com"
index := strings.Index(str,"簡(jiǎn)書(shū)")
fmt.Println("index = ",index) // index = 4
}
5.func IndexFunc(s string, f func(rune) bool) int
- 自定義超找
package main
import (
"fmt"
"strings"
)
func main() {
str := "www.簡(jiǎn)書(shū).com"
index := strings.IndexFunc(str,lookup)
fmt.Println("index = ",index) // index = 13
}
// 自定義超找的內(nèi)容
func lookup(ch rune)bool{
if ch == 'm' {
return true
}
return false
}
從右至左查找的函數(shù)
2.從右至左查找跟從左至右基本一樣
func LastIndex(s, sep string) int
func LastIndexByte(s string, c byte) int
func LastIndexAny(s, chars string) int
func LastIndexFunc(s string, f func(rune) bool) int
字符串包含
- 判斷字符串中是否包含指定的字符, 包含就返回ture 不含飯返回 false
1.func Contains(s, substr string) bool
判斷字符串中是否包含指定的
子串
注意點(diǎn)
- 會(huì)將指定子串看成一個(gè)
整體
查找
package main
import (
"fmt"
"strings"
)
func main() {
str := "www.reibang.com"
res := strings.Contains(str,"com")
fmt.Println("res = ",res) // res = true
}
2.func ContainsRune(s string, r rune) bool
- 判斷字符串中是否包含指定的
字符
----->(支持中文單字)
package main
import (
"fmt"
"strings"
)
func main() {
str := "www.reibang.com"
res := strings.ContainsRune(str,'s')
fmt.Println("res = ",res) // res = true
}
3.func ContainsAny(s, chars string) bool
判斷字符串中是否包含指定的
子串
注意點(diǎn)
- 會(huì)講指定查找的子串拆分開(kāi)來(lái)查找
- 支持中文超找
package main
import (
"fmt"
"strings"
)
func main() {
str := "www.reibang.com"
res := strings.ContainsAny(str,"xyzs")
fmt.Println("res = ",res) // res = true
}
4.func HasPrefix(s, prefix string) bool
- 功能 判斷字符串是否以指定字符串開(kāi)頭
package main
import (
"fmt"
"strings"
)
func main() {
str := "2018年報(bào)表1"
res := strings.HasPrefix(str,"2018")
fmt.Println("res = ",res) // res = true
}
5.func HasSuffix(s, suffix string) bool
- 功能 判斷字符串是否以指定字符串結(jié)尾
package main
import (
"fmt"
"strings"
)
func main() {
str := "如歌.mp3"
res := strings.HasSuffix(str,".mp3")
fmt.Println("res = ",res) // res = true
}
字符串比較
1. func Compare(a, b string) int
- 功能
- 判斷兩個(gè)字符串是否相等
- 如果相等返回0
- 如果不相等的情況下
第一個(gè)字符串>第二個(gè)字符串-----> 返回1
第一個(gè)字符串<第二個(gè)字符串-----> 返回-1
package main
import (
"fmt"
"strings"
)
func main() {
str := "123456"
str1 := "123456"
str2 := "123"
str3 := "124"
res := strings.Compare(str,str1)
res2 := strings.Compare(str1,str2)
res3 := strings.Compare(str2,str3)
fmt.Println("res = ",res) // res = 0
fmt.Println("res2 = ",res2) // res2 = 1
fmt.Println("res3 = ",res3) // res3 = -1
}
2. func EqualFold(s, t string) bool
- 作用 判斷兩個(gè)字符串是否相等,相等返回ture 不相等返回 false
package main
import (
"fmt"
"strings"
)
func main() {
str := "123456"
str1 := "123456"
str2 := "123"
res := strings.EqualFold(str,str1)
res2 := strings.EqualFold(str1,str2)
fmt.Println("res = ",res) // res = true
fmt.Println("res2 = ",res2) // res2 = false
}
字符串轉(zhuǎn)換
1.unc ToUpper(s string) string
- 作用 將指定的字符串所有字母變成大寫(xiě)的并返回一個(gè)新的字符串
package main
import (
"fmt"
"strings"
)
func main() {
str := "abcdefg"
str2 := strings.ToUpper(str)
fmt.Println(str2) // str2 = ABCDEFG
}
2.func ToLower(s string) string
- 作用 將指定的字符串所有字母變成小寫(xiě)的并返回一個(gè)新的字符串
package main
import (
"fmt"
"strings"
)
func main() {
str := "ABCDEFG"
str2 := strings.ToLower(str)
fmt.Println(str2) // str2 = abcdefg
}
3.func ToTitle(s string) string
- 作用 指定字符串轉(zhuǎn)換為大寫(xiě)
package main
import (
"fmt"
"strings"
)
func main() {
str := "hello world this is golang language"
str2 := strings.ToTitle(str)
fmt.Println(str2) // str2 = HELLO WORLD THIS IS GOLANG LANGUAGE
}
4.func ToUpperSpecial(_case unicode.SpecialCase, s string) string
5.func ToLowerSpecial(_case unicode.SpecialCase, s string) string
6.func ToTitleSpecial(_case unicode.SpecialCase, s string) string
7. func Title(s string) string
- 作用將字符串中的單詞首字母大寫(xiě) 需要用- 和空格 來(lái)將單詞隔開(kāi)
package main
import (
"fmt"
"strings"
)
func main() {
str := "hello world this is golang language"
str2 := strings.Title(str)
fmt.Println(str2) // str2 = Hello World This Is Golang Language
}
字符串的拆合
1. 字符串切割
-
func Split(s, sep string) []string
Split作用: 按指定字符來(lái)切割字符串, 分割后不包含指定的字符
package main
import (
"fmt"
"strings"
)
func main() {
str := "2018-09-29-15-59"
str1 := strings.Split(str,"-")
fmt.Println(sce) // str1 = [2018 09 29 15 59]
}
-
func SplitN(s, sep string, n int) []string
SplitN的作用 按照指定字符切割字符串 并指定切割乘 幾份
package main
import (
"fmt"
"strings"
)
func main() {
str := "2018-09-29-15-59"
str1 := strings.SplitN(str,"-",2)
fmt.Println(str1) // str1 = [2018 09-29-15-59]
}
-
func SplitAfter(s, sep string) []string
SplitAfter的作用按照 指定字符串切割原有字符串, 切割后包含指定的字符
package main
import (
"fmt"
"strings"
)
func main() {
str := "2018-09-29-15-59"
str1 := strings.SplitAfter(str,"-")
fmt.Println(str1) // str1 = [2018- 09- 29- 15- 59]
}
-
func SplitAfterN(s, sep string, n int) []string
SplitAfterN作用: 按照指定字符串切割原有字符串, 切割為指定的份數(shù), 切割完包含指定字符
package main
import (
"fmt"
"strings"
)
func main() {
str := "2018-09-29-15-59"
str1 := strings.SplitAfterN(str,"-",3)
fmt.Println(str1) // str1 = [2018- 09- 29-15-59]
}
2. 按照空格切割字符串
-
func Fields(s string) []string
Fields作用 按照空格來(lái)切割字符串 (連續(xù)多余的空格按一個(gè)空格處理)
package main
import (
"fmt"
"strings"
)
func main() {
str := "182 4030 6093"
str1 := strings.Fields(str)
fmt.Println(str1) // str1 = [182 4030 6093]
}
-
func FieldsFunc(s string, f func(rune) bool) []string
FieldsFunc作用 自定義分割字符串的標(biāo)準(zhǔn)
package main
import (
"fmt"
"strings"
)
func main() {
str := "2018/09/29"
str1 := strings.FieldsFunc(str,divisional)
fmt.Println(str1) // str1 = [2018 09 29]
}
func divisional(ch rune)bool{
if ch== '/' {
return true
}
return false
}
3.字符串合并
-
func Join(a []string, sep string) string
Join 按照指定字符拼接切片中的字符串
package main
import (
"fmt"
"strings"
)
func main() {
str := []string{"www","jianshu","com"}
str1 := strings.Join(str,".")
fmt.Println("str1 = ",str1) // str1 = www.reibang.com
}
4.重復(fù)生成字符串
-
func Repeat(s string, count int) string
Repeat的作用 就是將字符串復(fù)制成指定份數(shù)后組成一個(gè)新的字符串
package main
import (
"fmt"
"strings"
)
func main() {
str := "A"
str1 := strings.Repeat(str,3)
fmt.Println("str1 = ",str1) // str1 = AAA
}
5.替換字符串
-
func Replace(s, old, new string, n int) string
Replace 的作用就是將原先字符串中 指定的字符串替換成新的字符串 , 最后的一個(gè)參數(shù)指定替換幾次 如果是-1就是全部替換
package main
import (
"fmt"
"strings"
)
func main() {
str := "hello hello hello hello"
str1 := strings.Replace(str,"h","H",-1)
fmt.Println("str1 = ",str1) //str1 = Hello Hello Hello Hello
}
字符串清理
1.func Trim(s string, cutset string) string
1.1.func TrimFunc(s string, f func(rune) bool) string
- 作用 去除字符串前后指定的字符串
package main
import (
"fmt"
"strings"
)
func main() {
str := "hello Hello everyone hello"
str1 := strings.Trim(str,"hello")
fmt.Println("str1 = ",str1) //str1 = Hello everyone
}
2.func TrimLeft(s string, cutset string) string
2.1.func TrimLeftFunc(s string, f func(rune) bool) string -->(自定義從左去除指定字符串)
3.func TrimRight(s string, cutset string) string
3.1.func TrimRightFunc(s string, f func(rune) bool) string--->(自定義從右去除指定字符串)
- TrimLeft 只去除左邊指定去除的字符串
package main
import (
"fmt"
"strings"
)
func main() {
str := "hello Hello everyone"
str1 := strings.TrimLeft(str,"hello")
fmt.Println("str1 = ",str1) //str1 = Hello everyone
}
- TrimRight 只去處右邊指定去除的字符串
package main
import (
"fmt"
"strings"
)
func main() {
str := " Hello everyone hello"
str1 := strings.TrimRight(str,"hello")
fmt.Println("str1 = ",str1) //str1 = Hello everyone
}
7.func TrimSpace(s string) string
- 去除字符串前后的空格
package main
import (
"fmt"
"strings"
)
func main() {
str := " Hello World "
str1 := strings.TrimSpace(str)
fmt.Println("str1 = ",str1) //str1 = Hello everyone
}
8.func TrimPrefix(s, prefix string) string ---> 去除以指定字符開(kāi)頭的字符串
9.func TrimSuffix(s, suffix string) string ---> 去除以指定字符結(jié)尾的字符串