在Go语言中遍历字符串并从字符中生成字符串。

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

Iterating over go string and making string from chars in go

问题

我开始学习go,并且想要实现一些算法。我可以遍历字符串并获取字符,但这些字符是Unicode数字。

在go中如何将字符连接成字符串?你有一些参考资料吗?我在官方页面上找不到关于原始类型的任何信息。

英文:

I started learning go and I want to implement some algorithm. I can iterate over strings and then get chars, but these chars are Unicode numbers.

How to concatenate chars into strings in go? Do you have some reference? I was unable to find anything about primitives in official page.

答案1

得分: 9

使用range遍历字符串时,会得到Unicode字符,而使用索引遍历字符串时,会得到字节。请参阅规范中关于runesstrings以及它们的转换的部分。

正如The New Idiot提到的,可以使用+运算符来连接字符串。

从字符到字符串的转换有两种方式。你可以将byte(或字节序列)转换为字符串:

string(byte('A'))

或者你可以将rune(或符文序列)转换为字符串:

string(rune('µ'))

区别在于,符文表示Unicode字符,而字节表示8位值。

但所有这些都在我上面链接的规范的相应部分中提到了。它非常容易理解,你一定要阅读它。

英文:

Iterating over strings using range gives you Unicode characters while
iterating over a string using an index gives you bytes. See the spec for
runes and strings as well as their conversions.

As The New Idiot mentioned, strings can be concatenated using the +
operator
.

The conversion from character to string is two-fold. You can convert
a byte (or byte sequence) to a string:

string(byte('A'))

or you can convert a rune (or rune sequence) to a string:

string(rune('µ'))

The difference is that runes represent Unicode characters while bytes represent
8 bit values.

But all of this is mentioned in the respective sections of the spec I linked above.
It's quite easy to understand, you should definitely read it.

答案2

得分: 3

你可以直接将[]rune转换为字符串:

string([]rune{'h', 'e', 'l', 'l', 'o', '☃'})

http://play.golang.org/p/P9vKXlo47c

关于参考资料,它在Go规范的转换部分中,标题为“转换到和从字符串类型”的部分中。

http://golang.org/ref/spec#Conversions

至于拼接,你可能不想使用+运算符将每个字符都连接起来,因为这样会在底层执行大量的复制操作。如果你一次只获取一个符文,并且不构建一个中间符文切片,那么你很可能想使用bytes.Buffer,它有一个WriteRune方法用于这种情况。http://golang.org/pkg/bytes/#Buffer.WriteRune

英文:

you can convert a []rune to a string directly:

string([]rune{'h', 'e', 'l', 'l', 'o', '☃'})

http://play.golang.org/p/P9vKXlo47c

as for reference, it's in the Conversions section of the Go spec, in the section titled "Conversions to and from a string type"

http://golang.org/ref/spec#Conversions

as for concatenation, you probably don't want to concatenate every single character with the + operator, since that will perform a lot of copying under the hood. If you're getting runes in one at a time and you're not building an intermediate slice of runes, you most likely want to use a bytes.Buffer, which has a WriteRune method for this sort of thing. http://golang.org/pkg/bytes/#Buffer.WriteRune

答案3

得分: 2

使用 +

str:= str + "a"

你可以尝试像这样:

string1 := "abc"
character1 := byte('A')
string1 += string(character1)

甚至这个答案可能会有所帮助。

英文:

Use +

str:= str + "a"

You can try something like this :

string1 := "abc"
character1 := byte('A')
string1 += string(character1)

Even this answer might be of help.

答案4

得分: 0

definetly worth reading @nemo's post

使用range迭代字符串会得到Unicode字符,而使用索引迭代字符串会得到字节。请参阅符文和字符串以及它们的转换的规范。

可以使用+运算符连接字符串。

从字符到字符串的转换是双重的。您可以将字节(或字节序列)转换为字符串:

string(byte('A'))

或者您可以将符文(或符文序列)转换为字符串:

string(rune('µ'))
英文:

definetly worth reading @nemo's post

Iterating over strings using range gives you Unicode characters while iterating over a string using an index gives you bytes. See the spec for runes and strings as well as their conversions.

Strings can be concatenated using the + operator.

The conversion from character to string is two-fold. You can convert a byte (or byte sequence) to a string:

string(byte('A'))

or you can convert a rune (or rune sequence) to a string:

string(rune('µ'))

huangapple
  • 本文由 发表于 2013年6月26日 15:50:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/17314451.html
匿名

发表评论

匿名网友

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

确定