C#优化字符串操作(6)--把123456789转换为12-345-6789的3种方法
2007-06-19 16:10
483 查看
6.把123456789转换为12-345-6789的3种方法
方法一:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
string a = "123456789";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a = int.Parse(a).ToString("##-###-####");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Console.Write(a);
方法二:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
string a = "123456789";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a = a.Insert(5, "-").Insert(2, "-");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Console.Write(a);
方法三:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Collections.Generic;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Text.RegularExpressions;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace ConsoleApplication1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
class Program
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static void Main()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string a = "123456789";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Regex reg = new Regex(@"^(d{2})(d{3})(d{4})$");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
a = reg.Replace(a, "$1-$2-$3");
4000
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.Write(a);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
方法一:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
string a = "123456789";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a = int.Parse(a).ToString("##-###-####");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Console.Write(a);
方法二:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
string a = "123456789";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
a = a.Insert(5, "-").Insert(2, "-");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Console.Write(a);
方法三:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Collections.Generic;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Text.RegularExpressions;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace ConsoleApplication1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
class Program
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static void Main()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
......{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string a = "123456789";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Regex reg = new Regex(@"^(d{2})(d{3})(d{4})$");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
a = reg.Replace(a, "$1-$2-$3");
4000
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.Write(a);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
相关文章推荐
- 把123456789转换为12-345-6789的3种方法
- 把123456789转换为12-345-6789的3种方法
- 把123456789转换为12-345-6789的三种方法
- 编写高质量代码改善C#程序的157个建议[正确操作字符串、使用默认转型方法、却别对待强制转换与as和is]
- C#实现字符串转换成字节数组的简单实现方法
- C#中字符串转换成枚举类型的方法
- C#优化字符串操作
- Java字符串操作、基本运算方法等优化策略
- C#优化字符串操作
- C#操作字符串方法总结<转>
- C#优化字符串操作(4)--保留2位有效小数及四舍五入
- C#优化字符串操作
- C#优化字符串操作
- C#优化字符串操作
- c#操作字符串方法
- C#中对象,字符串,dataTable、DataReader、DataSet,对象集合转换成Json字符串方法。
- C#将字符串转换为整型的三种方法的总结
- 网友面试题:C#,不使用任何Parse,TryParse,Convert方法,自写函数将数字字符串转换成整数
- C#中字符串转换为计算公式,并进行计算的方法(自定义公式的计算)
- C#利用字符串方法把八进制数转换为十六进制数