在GO语言中,获取最后一个分割后的数组元素可以通过以下方式实现:

huangapple go评论122阅读模式
英文:

Get element of Array after last split in GO

问题

我不知道标题是否清楚,我将解释我的问题,

我有这段代码:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. s := "test-email-test@gmail.com"
  7. fields := []string{}
  8. last := 0
  9. for i, r := range s {
  10. if r == '_' || r == '-' {
  11. fmt.Printf("%q\n", fields)
  12. fields = append(fields, s[last:i+1])
  13. last = i + 1
  14. }
  15. }
  16. fmt.Printf("%q\n", fields)
  17. }

这段代码输出:

  1. ["test-" "email-"]

但是我不知道如何同时将最后一个单词"test"放入同一个数组中,以便得到:

  1. ["test-" "email-" "test"]

有人有想法吗?

非常感谢

英文:

I don't know if the title is clearly, i will explain my problem,

I have this piece of code :

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. s := "test-email-test@gmail.com"
  7. fields := []string{}
  8. last := 0
  9. for i, r := range s {
  10. if r == '_' || r == '-' {
  11. fmt.Printf("%q\n", fields)
  12. fields = append(fields, s[last:i+1])
  13. last = i + 1
  14. }
  15. }
  16. fmt.Printf("%q\n", fields)
  17. }

This code prints this :

  1. ["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 :

  1. ["test-" "email-" "test"]

Anyone has idea please ?

Thanks a lot

答案1

得分: 3

我不完全确定最终目标是什么,但如果目标是获取电子邮件地址中用户名部分的以-_分隔的字段列表,你可以使用strings.Splitstrings.FieldsFunc

你可以使用Split函数以@为分隔符获取用户名部分,然后使用FieldsFunc函数获取字段。

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func main() {
  7. s := "aaa-bbb-ccc@yyy.zzz"
  8. user := strings.Split(s, "@")[0] // 获取用户名部分
  9. fields := strings.FieldsFunc(user, func(c rune) bool {
  10. return c == '-' || c == '_'
  11. })
  12. fmt.Println(fields)
  13. }

playground

英文:

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.

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func main() {
  7. s := "aaa-bbb-ccc@yyy.zzz"
  8. user := strings.Split(s, "@")[0] // get the username part
  9. fields := strings.FieldsFunc(user, func(c rune) bool {
  10. return c == '-' || c == '_'
  11. })
  12. fmt.Println(fields)
  13. }

playground

答案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

你的循环将在 "@" 前面有 "-" 的情况下正确运行。
作为替代,你可以使用以下代码:

  1. s := string("test-email-test@gmail.com")
  2. fields := []string{}
  3. name := strings.Split(s, "@") // 从 "@" 分割
  4. name2 := strings.Split(name[0], "-") // 从 "-" 分割
  5. for i := 0; i < len(name2); i++ {
  6. fields = append(fields, name2[i])
  7. }
  8. fmt.Println(fields)

输出结果为:

  1. [test email test]
英文:

your loop will run correctly if there is "-" before the "@"
for alternative you may use

  1. s := string(&quot;test-email-test@gmail.com&quot;)
  2. fields := []string{}
  3. name := strings.Split(s, &quot;@&quot;) //separate from the &quot;@&quot;
  4. name2 := strings.Split(name[0], &quot;-&quot;) //separate from the &quot;-&quot;
  5. for i := 0; i &lt; len(name2); i++ {
  6. fields = append(fields, name2[i])
  7. }
  8. fmt.Println(fields)

output

  1. [test email test]

huangapple
  • 本文由 发表于 2022年4月12日 01:22:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/71831890.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定