《C#高级编程》读书笔记(六):字符串和正则表达式
2016-07-07 15:26
453 查看
1,创建字符串
字符串是一个不可变的数据类型,一旦对字符串对象进行了初始化,该字符串对象就不能改变了。所以,如果用字符串频繁进行文字处理,应用程序就会遇到严重的性能问题,这时需要采用StringBuilder类。
2,对自定义结构的格式化输出
实现IFormattable接口
测试一下:
输出:
||42||
(1.000000E+000,4.000000E+000,-5.000000E+000)
(1,4,-5)
3,正则表达式
字符串是一个不可变的数据类型,一旦对字符串对象进行了初始化,该字符串对象就不能改变了。所以,如果用字符串频繁进行文字处理,应用程序就会遇到严重的性能问题,这时需要采用StringBuilder类。
2,对自定义结构的格式化输出
实现IFormattable接口
struct Vector:IFormattable { public double x, y, z; public Vector(double x, double y, double z) : this() { this.x = x; this.y = y; this.z = z; } public Vector(Vector rhs) { x = rhs.x; y = rhs.y; z = rhs.z; } public string ToString(string format, IFormatProvider formatProvider) { if (format == null) { return ToString(); } switch (format.ToUpper()) { case "N": return "||" + Norm().ToString() + "||"; case "VE": return $"({x:E},{y:E},{z:E})"; default: return ToString(); } } private double Norm() { return x*x + y*y + z*z; } public override string ToString() { return "(" + x + "," + y + "," + z + ")"; } }
测试一下:
static void Main(string[] args) { var v1 = new Vector(1,4,-5); Console.WriteLine($"{v1,0:N}"); Console.WriteLine($"{v1,0:VE}"); Console.WriteLine(v1); Console.ReadKey(); }
输出:
||42||
(1.000000E+000,4.000000E+000,-5.000000E+000)
(1,4,-5)
3,正则表达式
const string myTest = @"This comprehensive comendium provides a broad and thorough investigation of all aspects of programming with ASP.NET.Entirely revised and updated for the fourth release of .NET,this book will give you the information you need to master ASP.NET and build a dynamic,sucessful,enterprise Web application."; const string pattern = @"\ba\S*ion\b"; MatchCollection myMatches = Regex.Matches(myTest, pattern, RegexOptions.IgnoreCase| RegexOptions.ExplicitCapture); foreach (Match nextMatch in myMatches) { Console.WriteLine(nextMatch.Index); }
相关文章推荐
- C#使用WebService和Session
- Visual Studio 2013 打包C#项目程序
- [C#] .NET4.0中使用4.5中的 async/await 功能实现异步
- c#中的linq二
- 浅谈C#抽象方法、虚方法、接口
- C# 遍历文件
- C#代码安装、卸载、监控Windows服务
- C#基础语言知识--Framework类库
- C# 6.0的字典(Dictionary)的语法
- C# 浏览文件夹地址
- 在.net中调用别人写的接口获得数据
- C#使用SSDB管理增量日志并提供查询
- C# datatable取某几列值
- C#获取机器码的方法详解(机器名,CPU编号,硬盘编号,网卡mac等)
- C#简单实现显示中文格式星期几的方法
- C#使用系统方法发送异步邮件完整实例
- c#中的delegate(委托)和event(事件)了
- 由浅到深讲解C#-LINQ
- C# 6.0的属性(Property)的语法与初始值详解
- C# 读取xls格式的文件