The relationship of 2018-08-16-string Rune byte

Source: Internet
Author: User

In the go language, the bottom string is stored in a byte array and cannot be changed.

For example, the s:="Go编程" fmt.Println(len(s)) output should be 8 because the Chinese characters are stored in 3 bytes.

len(string(rune('编')))The result is 3.

If we want to get what we want, we need to convert to rune slices and then use the built-in Len function as fmt.Println(len([]rune(s))) a result of 4.

So if you use string to store Unicode, if you have Chinese, the subscript is inaccessible because you can only get a byte. If you want to access Chinese, you still need to use Rune to slice it, so you can access it by following the table.

Rune is the alias of Int32 in Golang and is used to differentiate between character values and integer values. Using the above example, Rune can be understood as a value that can represent a Unicode encoded value of int, called a code point. But the Go Language abstracts this code point into rune.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.