Re: [go-nuts] Working with characters in strings
Sorry, I did not mean to say they are restricted to UTF8. I was answering the question "what is the relation between a rune and its byte representation", by saying that the []byte cast of a string (assuming the string only holds legitimate runes) is the UTF8 of the runes making it up. The original question was confounded by asking about "characters" which is a term that is pretty much only used by the reference in speaking of program text; the only exception I could find is speaking of Unicode replacement characters in talking about range iteration of strings. My point is that the relation between a rune and its byte representation is "UTF-8". I don't know whether the original question was using "character" to mean "byte" (the curse of C!) or something else. Thomas On Mon, Oct 31, 2016 at 5:13 PM Lars Seipel wrote: > On Mon, Oct 31, 2016 at 06:06:23PM +, 'Thomas Bushnell, BSG' via > golang-nuts wrote: > > Strings are specified to be UTF8, so if you cast a string to []byte you > > will see its UTF8 representation. > > They are not. A Go string may contain arbitrary bytes. Features like > for..range or conversions to rune slices won't be very useful then > (though still well-defined) but that's about it. > > Go source text and thus string literals are UTF-8. You might be thinking > of those? > > -ls > -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] Working with characters in strings
On Mon, Oct 31, 2016 at 06:06:23PM +, 'Thomas Bushnell, BSG' via golang-nuts wrote: > Strings are specified to be UTF8, so if you cast a string to []byte you > will see its UTF8 representation. They are not. A Go string may contain arbitrary bytes. Features like for..range or conversions to rune slices won't be very useful then (though still well-defined) but that's about it. Go source text and thus string literals are UTF-8. You might be thinking of those? -ls -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] Working with characters in strings
I don't know what you mean by "characters", since you're distinguishing them from runes. Do you mean bytes? Strings are specified to be UTF8, so if you cast a string to []byte you will see its UTF8 representation. Thomas On Sat, Oct 29, 2016 at 4:42 AM wrote: > > I understand that strings are immutable sequences of bytes and wanted to > know how to idiomatically work with the "characters" within a string. > > For example how should I approach the following operations > > >- Convert a string to a []string (slice or array). eg. "foobar" to >["f" "o" "o" "b" "a" "r"] > > >- Iterate through a string by "character" strings instead of runes, >for example: > > str := "foo" > for _,char := range "bar" { > str += char // is a cast string(char) required? > } > fmt.Println(str) // print "foobar" > > > > Also what is the relationship between a rune and it's byte representation? > > Any additional advice for working with characters of strings would be > appreciated as well. > > -- > 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+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Working with "characters" in strings
1. create the slice (ss := make([]string, n)) and fill up (ss[i] = s[a:b]), this will reuse the s's backing array, so use only the pointer to the backing array and a length. 2. rune is a unicode code point, an alias for int32. A string is an utf-8 encoded representation of a slice of runes: rr := []rune(s). `for _, r := range s` will range through the runes in the string. That utf-8 encoding may use at most 4 bytes for a code point, but uses exactly 1 byte per ASCII character. -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Working with "characters" in strings
I know that strings are an immutable sequence of bytes, so what would be the most idiomatic way to handle the following operations. - Convert a string to []string (slice) or [n]string (array). Eg. "foobar" to ["f" "o" "o" "b" "a" "r"] - Iterate through a string and evaluate the rune directly as a string str := "foo" for _,char := range "bar" { // should concatenate a character at a time str += char // requires cast string(char) correct? } Also what is the relationship between a "rune" and the "byte" that a string is composed of? If you have other advice for working with individual string "character" of a string for other scenarios I'd appreciate it. -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Working with characters in strings
I understand that strings are immutable sequences of bytes and wanted to know how to idiomatically work with the "characters" within a string. For example how should I approach the following operations - Convert a string to a []string (slice or array). eg. "foobar" to ["f" "o" "o" "b" "a" "r"] - Iterate through a string by "character" strings instead of runes, for example: str := "foo" for _,char := range "bar" { str += char // is a cast string(char) required? } fmt.Println(str) // print "foobar" Also what is the relationship between a rune and it's byte representation? Any additional advice for working with characters of strings would be appreciated as well. -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.