formatting string in MVC /C#

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

    My first thought is:

    String s = "731478718861993983";
    s = s.Insert(3,"-");
    s = s.Insert(8,"-");
    s = s.Insert(13,"-");
    s = s.Insert(18,"-");
    

    (don't remember if index is zero-based, in which case you should use my values -1) but there is probably some easier way to do this...

提交回复
热议问题