How to split '_' with one more any one character in golang?

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

How to split '_' with one more any one character in golang?

问题

如何使用任意一个字符来分割'_'?

例如,

var one = 6221c62c67bc2a98ec6f713b_h32 
-> strings.Split(one, "_h")

var one = 12345c62c67bc2a98ec6f723c_c32
-> strings.Split(one, "_c")

var one = 12345c62c67bc2a98ec6f723c_s32
-> strings.Split(one, "_s")

如何将它们组合起来?(使用正则表达式?还是其他想法)

strings.Split(one, "_??")
英文:

How to split '_' with any one character?

for Examples,

var one = 6221c62c67bc2a98ec6f713b_h32 
-> strings.Split(one, "_h")

var one = 12345c62c67bc2a98ec6f723c_c32
-> strings.Split(one, "_c")

var one = 12345c62c67bc2a98ec6f723c_s32
-> strings.Split(one, "_s")

How to combine these? (use regex? or any idea)

strings.Split(one, "_??")

答案1

得分: 1

你可以使用正则表达式:_[a-zA-Z],例如:

str := "6221c62c67bc2a98ec6f713b_h32"
a := regexp.MustCompile("_[a-zA-Z]")
fmt.Println(a.Split(str, 2))
英文:

You can use the regex: _[a-zA-Z], for eg

str := "6221c62c67bc2a98ec6f713b_h32"
a := regexp.MustCompile("_[a-zA-Z]")
fmt.Println(a.Split(str, 2))

答案2

得分: 0

你可以使用字符串的Replace方法:

strings.Replace("6221c62c67bc2a98ec6f713b_h32", "_h", " ", 1)
英文:

You can use string.Replace:

strings.Replace("6221c62c67bc2a98ec6f713b_h32", "_h", " ", 1)

huangapple
  • 本文由 发表于 2022年3月7日 10:00:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/71375716.html
匿名

发表评论

匿名网友

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

确定