Home > Backend Development > Golang > golang rune to string

golang rune to string

WBOY
Release: 2023-05-13 10:24:37
Original
1679 people have browsed it

When programming in Go language, we often need to convert Unicode encoding. One common conversion is to convert rune type to string type. In the Go language, the rune type is a Unicode character set with UTF-8 character encoding, while the string type is composed of a series of Unicode characters. This article will introduce how to convert rune type to string type.

1. Rune type and string type

In the Go language, the rune type is a 32-bit integer type used to represent a Unicode character. Usually, we use single quotes to represent a rune type. For example, the characters 'a', 'you', and '?' in single quotes can be expressed as rune types. In the Go language, we can use a for-range loop to traverse a string, and each rune type represents a Unicode character. For example:

str := "你好,世界!"
for i, r := range str {
    fmt.Printf("字符 %c 的Unicode编码是:%U
", r, r)
}
Copy after login

The output result is:

字符 你 的Unicode编码是:U+4F60
字符 好 的Unicode编码是:U+597D
字符 , 的Unicode编码是:U+FF0C
字符 世 的Unicode编码是:U+4E16
字符 界 的Unicode编码是:U+754C
字符 ! 的Unicode编码是:U+FF01
Copy after login

As you can see, each rune type represents a Unicode character, its type is int32, and its type is output in hexadecimal form. Unicode encoding.

The string type is composed of a series of Unicode characters. In Go language, we can also use a for-range loop to traverse a string to get each Unicode character. For example:

str := "你好,世界!"
for i, c := range str {
    fmt.Printf("第%d个字符是:%c
", i, c)
}
Copy after login

The output result is:

第0个字符是:你
第1个字符是:好
第2个字符是:,
第3个字符是:世
第4个字符是:界
第5个字符是:!
Copy after login

We can see that each string type is a string composed of Unicode characters, and each Unicode can be obtained separately through the for-range loop character.

2. Convert rune type to string type

To convert rune type to string type, we can use the built-in string() function. For example, the following example converts a rune type to a string type:

var r rune = 'a'
str := string(r)
fmt.Println(str)  //输出:a
Copy after login

Here we define a rune type variable r, its value is the Unicode encoding of the character 'a', convert it to the string type , get a string "a". Note that here we use the string() function to convert the rune type to the string type.

If we need to convert a sequence composed of multiple rune types into a string type, we can use the go language splicing operator. For example:

var runes []rune = []rune{'你', '好', ',', '世', '界', '!'}
str := string(runes)
fmt.Println(str)  //输出:你好,世界!
Copy after login

Here we define a slice runes containing multiple rune types, and use the string() function to convert it to the string type to get the string "Hello, world!".

3. Summary

This article introduces how to convert rune type to string type. In the Go language, the rune type represents a Unicode character, while the string type consists of multiple Unicode characters. Through the built-in string() function, we can convert the rune type to the string type. In actual programming, we can convert the sequence of rune type into string type as needed to realize the function of constructing a string.

The above is the detailed content of golang rune to string. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template