0

How can I convert this string into an array or slice in Golang ? Separators are unicode caracters \u001e and \uu1d.

inputString="\u001e456\u001dBernard Janv\u001d0022000\u001d250\u001d804\u001d1169\u001d\u001d168"
Laurie Ma
  • 35
  • 1
  • 5
  • Does this answer your question? [How to split a string and assign it to variables in Golang?](https://stackoverflow.com/questions/16551354/how-to-split-a-string-and-assign-it-to-variables-in-golang) –  Jan 19 '20 at 16:05
  • 1
    what is your expected output?? Do you want to remove these Unicode in your output?? – Prakash Kumar Jan 19 '20 at 16:06
  • Separators are only these two Unicode characters: \u001e and \u001d ? Or can there be others? – Tomor Jan 19 '20 at 19:27

1 Answers1

1

Use strings.Fields to split the string:

inputString := "\u001e456\u001dBernard Janv\u001d0022000\u001d250\u001d804\u001d1169\u001d\u001d168"
parts := strings.FieldsFunc(inputString, func(r rune) bool {
    return r == '\u001d' || r == '\u001e'
})
for _, part := range parts {
    fmt.Println(part)
}

https://play.golang.org/p/x_le2P3h8ry