Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

string

unicode

go

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?

like image 980
Knights Avatar asked Jul 06 '15 06:07

Knights


2 Answers

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
like image 98
icza Avatar answered Nov 13 '22 22:11

icza


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
like image 31
rhozet Avatar answered Nov 13 '22 21:11

rhozet