In C#, how can I use Regex.Replace to add leading zeroes (if possible)?

前端 未结 5 971
梦谈多话
梦谈多话 2021-01-18 07:15

I would like to add a certain number of leading zeroes to a number in a string. For example:

Input: \"page 1\", Output: \"page 001\" Input: \"page 12\", Ouput: \"pa

5条回答
  •  心在旅途
    2021-01-18 08:02

    string sInput = "page 1";
    //sInput = Regex.Replace(sInput, @"\d+", @"00$&");
    string result = Regex.Replace(sInput, @"\d+", me =>
    {
        return int.Parse(me.Value).ToString("000");
    });
    

提交回复
热议问题