Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

formatting string in MVC /C#

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.

like image 887
DotnetSparrow Avatar asked Apr 01 '11 10:04

DotnetSparrow


1 Answers

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
like image 65
HABJAN Avatar answered Sep 23 '22 09:09

HABJAN