formatting string in MVC /C#

后端 未结 10 2178
不知归路
不知归路 2021-02-19 03:15

I have a string 731478718861993983 and I want to get this 73-1478-7188-6199-3983 using C#. How can I format it like this ?

Thanks.

10条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-19 03:51

    By using regex:

        public static string FormatTest1(string num)
        {
            string formatPattern = @"(\d{2})(\d{4})(\d{4})(\d{4})(\d{4})";
            return Regex.Replace(num, formatPattern, "$1-$2-$3-$4-$5");
        }
    
        // test
        string test = FormatTest1("731478718861993983");
        // test result: 73-1478-7188-6199-3983
    

提交回复
热议问题