ds0678 2018-11-27 18:55
浏览 601
已采纳

字符串中的Golang递增数字(使用符文)

I have a string mixed with characters and numerals, but i want to increment the last character which happens to be a number, here is what i have, it works, but once i reach 10 rune goes to black since 10 decimal is zero, is there a better way to do this?

package main

import (
    "fmt"

)

func main() {
str := "version-1.1.0-8"
rStr := []rune(str)


last := rStr[len(rStr)-1]
rStr[len(rStr)-1] = last + 1


}

So this works for str := "version-1.1.0-8" = version-1.1.0-9 str := version-1.1.0-9 = version-1.1.0-

I understand why it is happening, but I dont know how to fix it

  • 写回答

1条回答 默认 最新

  • duanpendan8067 2018-11-27 19:17
    关注

    Your intention is to increment the number represented by the last rune, so you should do that: parse out that number, increment it as a number, and "re-encode" it into string.

    You can't operate on a single rune, as once the number reaches 10, it can only be represented using 2 runes. Another issue is if the last number is 19, incrementing it needs to alter the previous rune (and not adding a new rune).

    Parsing the numbers and re-encoding though is much easier than one might think.

    You can take advantage of the fmt package's fmt.Sscanf() and fmt.Sprintf() functions. Parsing and re-encoding is just a single function call.

    Let's wrap this functionality into a function:

    const format = "version-%d.%d.%d-%d"
    
    func incLast(s string) (string, error) {
        var a, b, c, d int
        if _, err := fmt.Sscanf(s, format, &a, &b, &c, &d); err != nil {
            return "", err
        }
    
        d++
        return fmt.Sprintf(format, a, b, c, d), nil
    }
    

    Testing it:

    s := "version-1.1.0-8"
    
    for i := 0; i < 13; i++ {
        var err error
        if s, err = incLast(s); err != nil {
            panic(err)
        }
        fmt.Println(s)
    }
    

    Output (try it on the Go Playground):

    version-1.1.0-9
    version-1.1.0-10
    version-1.1.0-11
    version-1.1.0-12
    version-1.1.0-13
    version-1.1.0-14
    version-1.1.0-15
    version-1.1.0-16
    version-1.1.0-17
    version-1.1.0-18
    version-1.1.0-19
    version-1.1.0-20
    version-1.1.0-21
    

    Another option would be to just parse and re-encode the last part, and not the complete version text. This is how it would look like:

    func incLast2(s string) (string, error) {
        i := strings.LastIndexByte(s, '-')
        if i < 0 {
            return "", fmt.Errorf("invalid input")
        }
    
        d, err := strconv.Atoi(s[i+1:])
        if err != nil {
            return "", err
        }
    
        d++
        return s[:i+1] + strconv.Itoa(d), nil
    }
    

    Testing and output is the same. Try this one on the Go Playground.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?