首页 > 其他分享 >Golang基础-Runes

Golang基础-Runes

时间:2023-02-20 11:56:34浏览次数:37  
标签:字节 Runes fmt 基础 Golang rune myRune myString string

rune与string

The rune type in Go is an alias for int32. Given this underlying int32 type, the rune type holds a signed 32-bit integer value. However, unlike an int32 type, the integer value stored in a rune type represents a single Unicode character.

myRune := '¿'
fmt.Printf("myRune type: %T\n", myRune)
// Output: myRune type: int32

fmt.Printf("myRune value: %v\n", myRune)
// Output: myRune value: 191

fmt.Printf("myRune Unicode character: %c\n", myRune)
// Output: myRune Unicode character: ¿

fmt.Printf("myRune Unicode code point: %U\n", myRune)
// Output: myRune Unicode code point: U+00BF

myRune := rune(0xbf)
myRune = 191
fmt.Printf("myRune Unicode character: %c\n", myRune)
// Output: myRune Unicode character: ¿

一个字符串是一个不可改变的字节序列。字符串可以包含任意的数据,包括byte值0,但是通常是用来包含人类可读的文本。文本字符串通常被解释为采用UTF8编码的Unicode码点 (rune)序列。
内置的len函数可以返回一个字符串中的字节数目(不是rune字符数目),索引操作s[i]返回第i 个字节的字节值,i必须满足0 ≤ i< len(s)条件约束。如果试图访问超出字符串索引范围的字节将会导致panic异常。
第i个字节并不一定是字符串的第i个字符,因为对于非ASCII字符的UTF8编码会要两个或多个 字节。

  • 一个字节8bit
  • int32是4个字节
  • UTF8每个文字的字节长度是变化的(1到4)
  • range遍历string的每个文字,而不是byte
  • 单引号是rune(一个字符,byte,int32),双引号是string(字符串)
myString := "❗hello"
for index, char := range myString {
  fmt.Printf("Index: %d\tCharacter: %c\t\tCode Point: %U\n", index, char, char)
}
// Output:
// Index: 0	Character: ❗		Code Point: U+2757
// Index: 3	Character: h		Code Point: U+0068
// Index: 4	Character: e		Code Point: U+0065
// Index: 5	Character: l		Code Point: U+006C
// Index: 6	Character: l		Code Point: U+006C
// Index: 7	Character: o		Code Point: U+006F

常用函数

UTF8解码

import "unicode/utf8"

for i := 0; i < len(s); {
    r, size := utf8.DecodeRuneInString(s[i:])
    fmt.Printf("%d\t%c\n", i, r)
    i += size
}

字符串的字节长度和文字长度

import "unicode/utf8"

myString := "❗hello"
stringLength := len(myString)
numberOfRunes := utf8.RuneCountInString(myString)

fmt.Printf("myString - Length: %d - Runes: %d\n", stringLength, numberOfRunes)
// Output: myString - Length: 8 - Runes: 6

类型转换

myRuneSlice := []rune{'e', 'x', 'e', 'r', 'c', 'i', 's', 'm'}
myString := string(myRuneSlice)
fmt.Println(myString)
// Output: exercism

myString := "exercism"
myRuneSlice := []rune(myString)
fmt.Println(myRuneSlice)
// Output: [101 120 101 114 99 105 115 109]
  • string是紧凑的不可改变到字节序列
  • 所谓到紧凑就是英文1字节,中文3字节等,紧凑排列
  • rune是int32的别名,把每个字符统一扩展到4字节
  • rune是单个字符,多个应使用rune slice,相当于int32的数组

Exercise

package logs

import "unicode/utf8"

// Application identifies the application emitting the given log.
func Application(log string) string {
	for _, c := range log {
		if c == '❗' {
			return "recommendation"
		} else if c == '

标签:字节,Runes,fmt,基础,Golang,rune,myRune,myString,string
From: https://www.cnblogs.com/roadwide/p/17136826.html

相关文章

  • golang 面向对象
    1.张老太养了两只猫:一只名字叫小白,今年3岁,白色。还有一只叫小花,今年100岁,花色。请编写一个程序,当用户输入小猫的名字时,就显示该猫的名字,年龄,颜色。如果用户输入的小猫名字......
  • 通信小白基础学习---MIMO技术入门,含码字,层映射,天线端口,预编码,PMI,rank,TM模式,波束赋形,空
    以下内容来源于B站up主“捻叶成剑”,如有侵权,请联系本人删除!载波聚合技术是增加带宽(拓宽车道),2*2MIMO是增加天线(增加车道为双车道)还有空分多址(实际应用不多)接收两......
  • 2023.02.20 - webscoket基础用法
    实例创建//创建实例,通过状态管理动态设置IPimportstorefrom'../store';letevaIp=store.getters.evaIp;constwebsocket=newWebSocket(`${'http://www.baidu......
  • 基础知识-数据包和帧
    什么是数据包和帧数据包和帧是小块数据,当它们组合在一起时,会形成更大的信息或消息。但是,它们在OSI模型中是两个不同的东西。帧位于第2层-数据链路层,这意味着没有IP......
  • IP地址基础
    IP地址分类A类:00000000-01111111:1-127网络数:126,127每个网络中的主机数:2^24-2默认子网掩码:255.0.0.0私网地址:10.0.0.0B类:10000000-10111111:128-19......
  • K8S-pod基础与分类
    一、Pod基础概念1.1Pod基础概念Pod是kubernetes中最小的资源管理组件,Pod也是最小化运行容器化应用的资源对象。一个Pod代表着集群中运行的一个进程。kubernetes中其他大......
  • html基础标签
    资料来源于:B站尚硅谷JavaWeb教程(全新技术栈,全程实战),本人才疏学浅,记录其笔记以供他日回顾视频链接<html> <head> <title>这是我的第一个网页</title> <metac......
  • 密码学基础概念
    裴蜀定理说明了对任何整数a、b和它们的最大公约数d,关于未知数x和y的线性不定方程(称为裴蜀等式):\(若a,b是整数,且gcd(a,b)=d,那么对于任意的整数x,y,ax+by都一定是d的倍数,特别......
  • CPP基础
    函数的默认参数:①需要放在参数列表的末尾②放在函数的声明才有效 使用const修饰的函数引用:①不仅仅是限定引用的值不能改变②本质上是创建的一个临时匿名变量,把实......
  • html基础介绍
    html基础介绍1、html结构<!DOCTYPEhtml> //声明为HTML5文档<htmllang="zh-CN"> //HTML页面的根节点 //lang=:"zh-CN"告诉浏览器,内容是中文的,无需翻译......