Increment a string with both letters and numbers

前端 未结 8 1839
故里飘歌
故里飘歌 2020-12-19 00:50

I have a string which i need to increment by 1 The string has both characters and numeric values.

The string layout i have is as follows \"MD00494\"

How woul

8条回答
  •  青春惊慌失措
    2020-12-19 00:57

    You need to find the position of the first digit in the string. Then split the string into 2 fields.

    0 1 2 3 4 5 6

    M D 0 0 4 9 4

    The first field will be the non numeric part "MD" The second field will be the numeric part "00494"

    Increment the numeric only part to "00495"

    You will lose the leading zero's so you'll need to pad your new number with the same amount of zero's once you've incremented.

    Then join the 2 fields.

提交回复
热议问题