import "fmt"
func main() {
email := "[email protected]"
atTrue := false
s := ""
for i := 0; i < len(email); i {
if atTrue {
s = string(email[i])
}
if string(email[i]) == "@" {
atTrue = true
}
}
fmt.Println(s)
}
電流輸出:gmail.com
期望輸出:域:gmail和 TLD:com
如何指示從某個字串到某個字串的回圈?
uj5u.com熱心網友回復:
使用strings.Split函式
email := "[email protected]"
split := strings.Split(email, "@")
split = strings.Split(split[1], ".")
fmt.Println("Domain:", split[0])
fmt.Println("TLD:", split[1])
可選地,您可以使用mail.ParseAddress函式驗證電子郵件字串
email := "[email protected]"
addr, err := mail.ParseAddress(email)
if err != nil {
log.Fatal(err)
}
如果 TLD 像這個 co.id 一樣,輸出仍然是 co.id 怎么辦?– @zacharyy
只需嘗試找到第一個index(.strings.Index )并使用一個來分隔字串
email := "[email protected]"
split := strings.Split(email, "@")
index := strings.Index(split[1], ".")
fmt.Println("Domain:", split[1][:index])
fmt.Println("TLD:", split[1][index 1:])
或使用regexp.Split函式
email := "[email protected]"
split := strings.Split(email, "@")
split = regexp.MustCompile("[.]").
Split(split[1], 2)
fmt.Println("Domain:", split[0])
fmt.Println("TLD:", split[1])
或strings.SplitN
email := "[email protected]"
split := strings.Split(email, "@")
split = strings.SplitN(split[1], ".", 2)
fmt.Println("Domain:", split[0])
fmt.Println("TLD:", split[1])
或strings.Cut
_, host, found := strings.Cut(email, "@")
if !found {
t.Fatal("fail cut to host (Domain TLD)")
}
domain, tld, found := strings.Cut(host, ".")
if !found {
t.Fatal("fail cut to Domain and TLD")
}
fmt.Println("Domain:", domain)
fmt.Println("TLD:", tld)
???????? PLAYGROUND_
基準
const (
quantity = 10000
userLength = 10
domain = "gmail.com"
)
var (
letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
emails = make([]string, 0, quantity)
seeded = rand.New(rand.NewSource(time.Now().UnixMilli()))
)
for i := 0; i < quantity; i {
user := make([]rune, userLength)
for i := range user {
user[i] = letterRunes[seeded.Intn(len(letterRunes))]
}
emails = append(emails, fmt.Sprintf("%s@%s", string(user), domain))
}
b.Run("strings.Split", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
var domain, tld string
for i := 0; i < b.N; i {
for _, email := range emails {
domain, tld = stringsSplit(email)
}
}
_, _ = domain, tld
})
b.Run("strings.SplitN", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
var domain, tld string
for i := 0; i < b.N; i {
for _, email := range emails {
domain, tld = stringsSplitN(email)
}
}
_, _ = domain, tld
})
b.Run("strings.Index strings.Split", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
var domain, tld string
for i := 0; i < b.N; i {
for _, email := range emails {
domain, tld = stringsIndexAndStringsSplit(email)
}
}
_, _ = domain, tld
})
b.Run("regexp.Split", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
var domain, tld string
for i := 0; i < b.N; i {
for _, email := range emails {
domain, tld = regexpSplit(email)
}
}
_, _ = domain, tld
})
b.Run("strings.Cut", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
var domain, tld string
for i := 0; i < b.N; i {
for _, email := range emails {
domain, tld = stringsCut(email)
}
}
_, _ = domain, tld
})
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Benchmark
Benchmark/strings.Split
Benchmark/strings.Split-12 752 1563851 ns/op 640002 B/op 20000 allocs/op
Benchmark/strings.SplitN
Benchmark/strings.SplitN-12 805 1463329 ns/op 640003 B/op 20000 allocs/op
Benchmark/strings.Index strings.Split
Benchmark/strings.Index strings.Split-12 1416 858783 ns/op 320000 B/op 10000 allocs/op
Benchmark/regexp.Split
Benchmark/regexp.Split-12 85 14605240 ns/op 11088513 B/op 160024 allocs/op
Benchmark/strings.Cut
Benchmark/strings.Cut-12 6597 180579 ns/op 0 B/op 0 allocs/op
PASS
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/525379.html
標籤:细绳去
