dongqin1819 2015-08-27 21:20
浏览 32
已采纳

如何创建重复的字母字符串?

How do I create a bunch of alphabetic strings so that the first is "a", the 26th is "z", 27th is "aa"....e.g.:

a
b
c
d
...
...
...
x
y
z
aa
ab
ac
ad
...
...
...
ax
ay
az
aaa
aab
aac
...
...
...
aax
aay    
aaz
aaaa
aaab
...
...

I have the following playground code, but number 27 is "za" (s/b "aa"). I want to ultimately have the ability to create an infinite-length string w/ that pattern.

  • 写回答

2条回答 默认 最新

  • dongzaobei0942 2015-08-27 21:47
    关注

    Below is a rewrite of your function that should do the trick. You'll note that is uses the fact that the letters A-Z have a sequential unicode numbering, meaning the alphabet does not have to be stored.

    func NextAlias(last string) string {
        if last == "" {
            return "a"
        } else if last[len(last)-1] == 'z' {
            return last[:len(last)-1] + "aa"
        } else {
            return last[:len(last)-1] + string(last[len(last)-1] + 1)
        }
    }
    

    https://play.golang.org/p/Y2ViJKs_T4

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题