Increment a string with both letters and numbers

前端 未结 8 1850
故里飘歌
故里飘歌 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

    string sDispatchNo = "MS00914";
    var pattern = @"^[a-zA-Z]+";
    var strPart = Regex.Match(sDispatchNo, pattern).Value;
    var noPart = Regex.Replace(sDispatchNo, pattern, "");
    var no = int.Parse(noPart);
    var length = noPart.Length;
    length = (no + 1)/(Math.Pow(10,length)) == 1 ? length + 1 : length; 
    var output = strPart + (no + 1).ToString("D" + length);
    

提交回复
热议问题