I'm not sure why you are using strings.Index(s[6:], "/")
when there is no /
character in your original string, but you can replace the and -
characters in a few ways:
With your original attempt of split and join twice:
msg := "Time Server Type Class Method-Message"
msg = strings.Join(strings.Split(msg, " "), "|")
msg = strings.Join(strings.Split(msg, "-"), "|")
Or slightly more efficiently, by only joining once:
msg := "Time Server Type Class Method-Message"
parts := strings.Split(msg, " ")
parts = append(parts[:4], strings.Split(parts[4], "-")...)
newMsg := strings.Join(parts, "|")
Or with multiple calls to strings.Replace
:
newMsg := strings.Replace(strings.Replace(msg, " ", "|", -1), "-", "|", -1)
Or with a single regular expression:
newMsg := regexp.MustCompile(`[ -]`).ReplaceAllString(msg, "|")
출처 :
https://stackoverflow.com/questions/37924371/strings-split-and-join