Convert strings -> runes -> slice of runes -> string (UTF-8)

5,527 views
Skip to first unread message

Ken GoNoob

unread,
May 22, 2014, 12:45:13 PM5/22/14
to golan...@googlegroups.com
I'm trying to get a substring out of a string which may contain UTF-8 characters. I think I've got it correct to the point where I have a list of runes
representing the string as a slice: []int. Is there a better way to convert it back to a string than iterating through the slice and converting to string 1 char/rune at a time?
Ken

gardene...@gmail.com

unread,
May 22, 2014, 1:15:15 PM5/22/14
to golan...@googlegroups.com

Ken MacDonald

unread,
May 22, 2014, 2:16:44 PM5/22/14
to golang-nuts
Ah, nevermind. Can get it all in a single stmt:

x := string([]rune(mysring)[4:11])

Wrapped this with a check to see that mystring is a valid UTF-8 string.


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

nicolas...@gmail.com

unread,
May 22, 2014, 2:58:39 PM5/22/14
to golan...@googlegroups.com
A substring function can be something like this:

http://play.golang.org/p/P5yK-6fhB8

Reply all
Reply to author
Forward
0 new messages