英文:
Get element of Array after last split in GO
问题
我不知道标题是否清楚,我将解释我的问题,
我有这段代码:
package main
import (
"fmt"
)
func main() {
s := "test-email-test@gmail.com"
fields := []string{}
last := 0
for i, r := range s {
if r == '_' || r == '-' {
fmt.Printf("%q\n", fields)
fields = append(fields, s[last:i+1])
last = i + 1
}
}
fmt.Printf("%q\n", fields)
}
这段代码输出:
["test-" "email-"]
但是我不知道如何同时将最后一个单词"test"放入同一个数组中,以便得到:
["test-" "email-" "test"]
有人有想法吗?
非常感谢
英文:
I don't know if the title is clearly, i will explain my problem,
I have this piece of code :
package main
import (
"fmt"
)
func main() {
s := "test-email-test@gmail.com"
fields := []string{}
last := 0
for i, r := range s {
if r == '_' || r == '-' {
fmt.Printf("%q\n", fields)
fields = append(fields, s[last:i+1])
last = i + 1
}
}
fmt.Printf("%q\n", fields)
}
This code prints this :
["test-" "email-"]
But i don't know how can I put in the same time the last word, "test" in the same array, so that it gives :
["test-" "email-" "test"]
Anyone has idea please ?
Thanks a lot
答案1
得分: 3
我不完全确定最终目标是什么,但如果目标是获取电子邮件地址中用户名部分的以-
或_
分隔的字段列表,你可以使用strings.Split
和strings.FieldsFunc
。
你可以使用Split
函数以@
为分隔符获取用户名部分,然后使用FieldsFunc
函数获取字段。
package main
import (
"fmt"
"strings"
)
func main() {
s := "aaa-bbb-ccc@yyy.zzz"
user := strings.Split(s, "@")[0] // 获取用户名部分
fields := strings.FieldsFunc(user, func(c rune) bool {
return c == '-' || c == '_'
})
fmt.Println(fields)
}
英文:
I'm not entirely sure what the end goal is, but if it is to get a list of fields separated by -
or _
in the username part of an email address, you may want to just use strings.Split and strings.FieldsFunc.
You can just Split
on @
to get the username part, then use FieldsFunc
to get the fields.
package main
import (
"fmt"
"strings"
)
func main() {
s := "aaa-bbb-ccc@yyy.zzz"
user := strings.Split(s, "@")[0] // get the username part
fields := strings.FieldsFunc(user, func(c rune) bool {
return c == '-' || c == '_'
})
fmt.Println(fields)
}
答案2
得分: 0
你的字符串切割标准是找到特定的字符。最后一段不符合这个规则,除非你使用像"a-b-c-"这样的字符串。
建议:检查最后一个字符后是否还有内容,并在for循环之后将其添加进去。
英文:
Your criteria to cut a string is to find a certain char. The last piece does not match this rule except if you use a string like “a-b-c-“
Advice: check if there is something after last and append it after the for-loop
答案3
得分: 0
你的循环将在 "@" 前面有 "-" 的情况下正确运行。
作为替代,你可以使用以下代码:
s := string("test-email-test@gmail.com")
fields := []string{}
name := strings.Split(s, "@") // 从 "@" 分割
name2 := strings.Split(name[0], "-") // 从 "-" 分割
for i := 0; i < len(name2); i++ {
fields = append(fields, name2[i])
}
fmt.Println(fields)
输出结果为:
[test email test]
英文:
your loop will run correctly if there is "-" before the "@"
for alternative you may use
s := string("test-email-test@gmail.com")
fields := []string{}
name := strings.Split(s, "@") //separate from the "@"
name2 := strings.Split(name[0], "-") //separate from the "-"
for i := 0; i < len(name2); i++ {
fields = append(fields, name2[i])
}
fmt.Println(fields)
output
[test email test]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论