Go语言中与JavaScript的charCode()方法等效的方法是什么?

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

Go lang's equivalent of charCode() method of JavaScript

问题

在Go语言中,要获取相同字符串/字符的数值Unicode值,可以使用rune类型和fmt.Printf函数。下面是一个示例代码:

package main

import "fmt"

func main() {
    str := "s"
    unicodeValue := int([]rune(str)[0])
    fmt.Printf("%d\n", unicodeValue)
}

在上面的代码中,我们首先将字符串s转换为rune类型的切片,然后取切片中的第一个元素,并将其转换为整数类型。最后,使用fmt.Printf函数打印出数值Unicode值。

请注意,Go语言中的字符串是以UTF-8编码存储的,因此使用rune类型可以确保正确获取Unicode值。

英文:

The charCodeAt() method in JavaScript returns the numeric Unicode value of the character at the given index, e.g.

"s".charCodeAt(0) // returns 115

How would I go by to get the numeric unicode value of the the same string/letter in Go?

答案1

得分: 25

在Go语言中,字符类型是rune,它是int32的别名,因此它已经是一个数字,只需打印它即可。

你仍然需要一种方法来获取指定位置的字符。最简单的方法是将string转换为[]rune,然后可以对其进行索引。要将string转换为rune,只需使用类型转换[]rune("some string")

fmt.Println([]rune("s")[0])

输出:

115

如果你想将其作为字符打印出来,可以使用%c格式字符串:

fmt.Println([]rune("absdef")[2])      // 也会打印出 115
fmt.Printf("%c", []rune("absdef")[2]) // 打印出 s

还要注意,for range循环遍历string时会遍历字符串的每个字符,因此你也可以使用它。这比将整个string转换为[]rune更高效:

i := 0
for _, r := range "absdef" {
    if i == 2 {
        fmt.Println(r)
        break
    }
    i++
}

注意,计数器i必须是一个独立的计数器,它不能是循环迭代变量,因为for range返回的是字节位置而不是rune索引(如果string包含UTF-8表示中的多字节字符,则索引将不同)。

将其封装为一个函数:

func charCodeAt(s string, n int) rune {
    i := 0
    for _, r := range s {
        if i == n {
            return r
        }
        i++
    }
    return 0
}

Go Playground上尝试一下。

还要注意,Go中的string[]byte的形式存储在内存中,它是文本的UTF-8编码字节序列(阅读博文Go中的字符串、字节、符文和字符了解更多信息)。如果你保证string中使用的字符的代码小于127,你可以直接使用字节。也就是说,在Go中索引一个string会索引它的字节,所以例如"s"[0]'s'的字节值,即115

fmt.Println("s"[0])      // 打印出 115
fmt.Println("absdef"[2]) // 打印出 115
英文:

The character type in Go is rune which is an alias for int32 so it is already a number, just print it.

You still need a way to get the character at the specified position. Simplest way is to convert the string to a []rune which you can index. To convert a string to runes, simply use the type conversion []rune("some string"):

fmt.Println([]rune("s")[0])

Prints:

115

If you want it printed as a character, use the %c format string:

fmt.Println([]rune("absdef")[2])      // Also prints 115
fmt.Printf("%c", []rune("absdef")[2]) // Prints s

Also note that the for range on a string iterates over the runes of the string, so you can also use that. It is more efficient than converting the whole string to []rune:

i := 0
for _, r := range "absdef" {
	if i == 2 {
		fmt.Println(r)
		break
	}
	i++
}

Note that the counter i must be a distinct counter, it cannot be the loop iteration variable, as the for range returns the byte position and not the rune index (which will be different if the string contains multi-byte characters in the UTF-8 representation).

Wrapping it into a function:

func charCodeAt(s string, n int) rune {
	i := 0
	for _, r := range s {
		if i == n {
			return r
		}
		i++
	}
	return 0
}

Try these on the Go Playground.

Also note that strings in Go are stored in memory as a []byte which is the UTF-8 encoded byte sequence of the text (read the blog post Strings, bytes, runes and characters in Go for more info). If you have guarantees that the string uses characters whose code is less than 127, you can simply work with bytes. That is indexing a string in Go indexes its bytes, so for example "s"[0] is the byte value of 's' which is 115.

fmt.Println("s"[0])      // Prints 115
fmt.Println("absdef"[2]) // Prints 115

答案2

得分: 5

在Golang中,字符串在内部是一个8位字节数组。因此,每个字节都代表一个ASCII值。

str := "abc"
byteValue := str[0]
intValue := int(byteValue)
fmt.Println(byteValue) // 97
fmt.Println(intValue) // 97

英文:

Internally string is a 8 bit byte array in golang. So every byte will represent the ascii value.

str:="abc"
byteValue := str[0]
intValue := int(byteValue)
fmt.Println(byteValue)//97
fmt.Println(intValue)//97

huangapple
  • 本文由 发表于 2015年7月6日 14:49:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/31239330.html
匿名

发表评论

匿名网友

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

确定