Convert a charAtIndex of string to ASCII equivalent int value

32 views
Skip to first unread message

Amarjeet Anand

unread,
Dec 21, 2019, 11:23:28 AM12/21/19
to golan...@googlegroups.com
Hi
I have a set of strings(ASCII), that i want to assign to a string array(of cap 128).
The position of the string in the array is decided by the ASCII value of the first char of the string.
Like...

strArr := [128]string{}
strA := "A string"
strB := "B string"
strArr[65] = strA // since strA started with 'A' & ASCII('A') = 65
strArr[66] = strB // since strB started with 'B' & ASCII('B') = 66

There is one solution of using utf8.DecodeRuneInString, like ...
r, _ := utf8.DecodeRuneInString(strA)
strArr[r] = strA

Is it possible to time optimise this solution?


pierre...@gmail.com

unread,
Dec 21, 2019, 12:11:06 PM12/21/19
to golang-nuts
You can do something like this: https://play.golang.org/p/J7-N8_UDlL8
Reply all
Reply to author
Forward
0 new messages