Golang数字的字母表示 [英] Golang Alphabetic representation of a number
问题描述
有没有简单的方法将数字转换为字母?
例如,
3
=>
C
和 23
=> W
?
所有可以在去游乐场尝试。
Number - > rune
只需将数字添加到常量'A ' - 1
所以添加 1
就可以得到'A'
,并添加 2
您可以得到'B'
等:
func toChar(i int)rune {
return rune('A' - 1 + i)
}
测试它:
for _,i:= range [] int {1,2,23,26} {
fmt.Printf(%d%q \ n,i,toChar(i))
}
$ c $输出: 1'A'>
>
2'B'
23'W'
26'Z'
数字 - > 字符串
或者如果您想将它作为字符串
$ b $ pre $ func toCharStr(i int)string {
return string('A' - 1 + i )
}
输出:
1A
2B
23W
26Z
最后一个(将数字转换为 string
)记录在规范:与字符串类型的转换:
将带符号或无符号整数值转换为字符串类型将生成一个包含整数的UTF-8表示形式的字符串。
Number - > string
(cached)
如果您需要很多次这样做,在a中存储字符串
是有利的例如,n数组,然后返回字符串
:
var arr =字符串{A,B,C,D,E,F,G,H,I,J, K,L,M,
,N,O,P,Q,R,S,T,U,V ,W,X,Y,Z}
func toCharStrArr(i int)string {
return arr [i-1]
}
注意:切片(而不是数组)也可以。
注意#2:如果你添加一个虚拟的第一个字符,你可以改进它,所以你不必从<$ c中减去 1
$ b
var arr = [...] string {。, A,B,C,D,E,F,G,H,I,J,K,L,M ,$,O,P,Q,R,S,T,U,V,W,X Y,Z}
func toCharStrArr(i int)string {return arr [i]}
Number - > string
(切分字符串
常量)
另一个有趣的解决方案是:
const abc =ABCDEFGHIJKLMNOPQRSTUVWXYZ
func toCharStrConst(i int)string {
return abc [i-1: i]
}
切分字符串
是高效的:新的字符串
将共享后备数组(可以这样做,因为 string
s是不可变的) 。
Is there an easy way to convert a number to a letter?
For example,
3
=> "C"
and 23
=> "W"
?
For simplicity range check is omitted from below solutions.
They all can be tried on the Go Playground.
Number -> rune
Simply add the number to the const 'A' - 1
so adding 1
to this you get 'A'
, adding 2
you get 'B'
etc.:
func toChar(i int) rune {
return rune('A' - 1 + i)
}
Testing it:
for _, i := range []int{1, 2, 23, 26} {
fmt.Printf("%d %q\n", i, toChar(i))
}
Output:
1 'A'
2 'B'
23 'W'
26 'Z'
Number -> string
Or if you want it as a string
:
func toCharStr(i int) string {
return string('A' - 1 + i)
}
Output:
1 "A"
2 "B"
23 "W"
26 "Z"
This last one (converting a number to string
) is documented in the Spec: Conversions to and from a string type:
Converting a signed or unsigned integer value to a string type yields a string containing the UTF-8 representation of the integer.
Number -> string
(cached)
If you need to do this a lot of times, it is profitable to store the strings
in an array for example, and just return the string
from that:
var arr = [...]string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
func toCharStrArr(i int) string {
return arr[i-1]
}
Note: a slice (instead of the array) would also be fine.
Note #2: you may improve this if you add a dummy first character so you don't have to subtract 1
from i
:
var arr = [...]string{".", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
func toCharStrArr(i int) string { return arr[i] }
Number -> string
(slicing a string
constant)
Also another interesting solution:
const abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
func toCharStrConst(i int) string {
return abc[i-1 : i]
}
Slicing a string
is efficient: the new string
will share the backing array (it can be done because string
s are immutable).
这篇关于Golang数字的字母表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!