Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use rune to obtain character length in go language

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to use rune to obtain character length in go language". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use rune to obtain character length in go language" can help you solve the problem.

Rune is a special data type in the Go language. It is an alias for int32 and is equivalent to int32 in almost all respects. It is used to distinguish between character values and integer values. The official explanation is as follows:

/ / rune is an alias for int32 and is equivalent to int32 in all ways. Aliases for It is// used, by convention, to distinguish character values from integer values.//int32, which are equivalent in almost all respects to int32//. It is used to distinguish between character values and integer values type rune = int32.

Let's take a look at it through an example:

Package mainimport "fmt" func main () {var str = "hello Hello" fmt.Println ("len (str):", len (str))}

Let's guess the result, hello5 characters + 1 space + 3 Chinese characters, it should be 9 characters, the length is 9, but let's execute it.

The result print is 15. Why is that?

The bottom layer of string in golang is implemented through the byte array. Chinese characters occupy 2 bytes under unicode and 3 bytes under utf-8 encoding, while the default golang encoding happens to be utf-8.

So the calculated length is equal to 5'1'3'3'15.

If we need to calculate the length of the string instead of the number of underlying bytes, we can use the following method:

Package mainimport ("fmt"unicode/utf8") func main () {var str = "hello Hello" / / golang the bottom layer of string is implemented through the byte array Seat direct len is actually calculated by byte length, so a Chinese character occupies three bytes and calculates three lengths fmt.Println ("len (str):", len (str)) / / 15 / / the following two can get the string length of str / / 1. The unicode/utf8 package in golang provides a method to obtain the length with utf-8 fmt.Println ("RuneCountInString:" Utf8.RuneCountInString (str)) / / 2. Processing unicode characters fmt.Println ("rune:", len ([] rune (str) via rune type}

The running results are as follows:

There is another one above the rune definition, byte = uint8

/ / byte is an alias for uint8 and is equivalent to uint8 in all ways. It is// used, by convention, to distinguish byte values from 8-bit unsigned// integer values.type byte = uint8

Byte is equivalent to int8 and is often used to deal with ascii characters

Rune is equivalent to int32 and is often used to handle unicode or utf-8 characters

This is the end of the introduction to "how to use rune to obtain character length in the go language". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report