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

前端 未结 2 571
故里飘歌
故里飘歌 2021-01-11 18:15

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

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


        
相关标签:
2条回答
  • 2021-01-11 18:49

    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
    
    0 讨论(0)
  • 2021-01-11 18:54

    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
    
    0 讨论(0)
提交回复
热议问题