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

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

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.Splitstrings.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)
}

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.

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)
}

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

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

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(&quot;test-email-test@gmail.com&quot;)

fields := []string{}

name := strings.Split(s, &quot;@&quot;) //separate from the &quot;@&quot;

name2 := strings.Split(name[0], &quot;-&quot;) //separate from the &quot;-&quot;

for i := 0; i &lt; len(name2); i++ {
	fields = append(fields, name2[i])
}

fmt.Println(fields)

output

[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:

确定