formatting string in MVC /C#

后端 未结 10 1973
不知归路
不知归路 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:31

    Here's how I'd do it; it'll only work if you're storing the numbers as something which isn't a string as they're not able to be used with format strings.

    string numbers = "731478718861993983";
    string formattedNumbers = String.Format("{0:##-####-####-####-####}", long.Parse(numbers));
    

    Edit: amended code, since you said they were held as a string in your your original question

提交回复
热议问题