把123456789转换为12-345-6789的三种方法

方法一:

            string str = 123456789;
            str = int.Parse(str).ToString("##-###-####");
方法二:
            string str = "123456789";
            str = str.Insert(5,"-");
            str = str.Insert(2, "-");
方法三:
首先引入命名空间:using System.Text.RegularExpressions
            //Regex reg = new Regex(@"^(\d{2})(\d{3})(\d{4})$");
或者
            Regex reg = new Regex(@"^(\d{2})(\d{3})(\d{4})$");
            str = reg.Replace(str,"$1-$2-$3");
posted @ 2013-01-05 18:26  程序有Bug  阅读(481)  评论(0编辑  收藏  举报