已阅读5页,还剩14页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
9 Strings and Regular Expressions,Shuai L 2012.9.5,0. Introduction,The string keyword in C# actually refers to the System.String .NET base class. System.Text System.Text.StringBuilder IFormatProvider, IFormattable System.Text.RegularExpressions,1. System.String,System.String is a class specifically designed to store a string and allow a large number of operations on the string. You can concatenate strings using operator overloads string message1 = “Hello“; / returns “Hello“ message1 += “, There“; / returns “Hello, There“ string message2 = message1 + “!“; / returns “Hello, There!“ C# also allows extraction of a particular character using an indexer-like syntax string message = “Hello“; char char4 = message4; / returns o. Note the string is zero-indexed,1. System.String,The key methods of System.String Compare, CompareOrdinal Concat, CopyTo, Insert, Join, Replace, Split, Substring Format IndexOf, IndexOfAny, LastIndexOf, LastIndexOfAny PadLeft, PadRight ToLower, ToUpper, Trim,1. System.String,The String class has a shortcoming that makes it very inefficient for making repeated modifications to a given string it is actually an immutable data type, which means that after you initialize a string object, that string object can never change. The methods and operators that appear to modify the contents of a string actually create new strings, copying across the contents of the old string if necessary.,1. System.String,string greetingText = “Hello from all the guys at Wrox Press. “; /39 chs greetingText += “We do hope you enjoy this book as much as we enjoyed writing it.“; / 39+65-1=103 chs for(int i = z; i = a; i-) char old1 = (char)i; char new1 = (char)(i+1); greetingText = greetingText.Replace(old1, new1); for(int i = Z; i =A; i-) Console.WriteLine(“Encoded:n“ + greetingText);,2. System.Text.StringBuilder,The StringBuilder normally allocates more memory than is actually needed. Length which indicates the length of the string that it actually contains Capacity which indicates the maximum length of the string in the memory allocation Any modifications to the string take place within the block of memory assigned to the StringBuilder instance.,2. System.Text.StringBuilder,StringBuilder greetingBuilder = new StringBuilder(“Hello from all the guys at Wrox Press. “, 150); greetingBuilder.AppendFormat(“We do hope you enjoy this book as much as we enjoyed writing it“); for(int i = z; i=a; i-) char old1 = (char)i; char new1 = (char)(i+1); greetingBuilder = greetingBuilder.Replace(old1, new1); for(int i = Z; i=A; i-) Console.WriteLine(“Encoded:n“ + greetingBuilder);,2. System.Text.StringBuilder,The key methods of System.Text.StringBuilder Append, AppendFormat Insert, Remove, Replace ToString,3. Format Strings,Console.WriteLine() just passes the entire set of parameters to the static method, String.Format(). Console.WriteLine(“The double is 0,10:E and the int contains 1“, d, i); The implementation of the three-parameter overload of WriteLine() basically does this: public void WriteLine(string format, object arg0, object arg1) this.WriteLine(string.Format(this.FormatProvider, format , new objectarg0, arg1); ,3. Format Strings,String.Format() now needs to construct the final string by replacing each format specifier with a suitable string representation of the corresponding object. However, as you saw earlier, for this process of building up a string you need a StringBuilder instance rather than a string instance.,3. Format Strings,In this example, a StringBuilder instance is created and initialized with the first known portion of the string, the text “The double is”. Next, the StringBuilder.AppendFormat() method is called, passing in the first format specifier, 0,10:E, as well as the associated object, double, to add the string representation of this object to the string object being constructed. This process continues with StringBuilder.Append() and StringBuilder.AppendFormat() being called repeatedly until the entire formatted string has been obtained.,3. Format Strings,StringBuilder.AppendFormat() has to figure out how to format the object. First, it probes the object to find out whether it implements an interface in the System namespace called IFormattable. You can determine this quite simply by trying to cast an object to this interface and seeing whether the cast succeeds, or by using the C# is keyword. If this test fails, AppendFormat() calls the objects ToString() method, which all objects either inherit from System.Object or override.,3. Format Strings,4. IFormattable Interface,interface IFormattable string ToString(string format, IFormatProvider formatProvider); ,5. Vector,struct Vector: IFormattable public double x, y, z; public string ToString(string format, IFormatProvider formatProvider) if (format = null) return ToString(); string formatUpper = format.ToUpper(); switch (formatUpper) ,5. Vector,switch (formatUpper) case “N“: return “| “ + Norm().ToString() + “ |“; case “VE“: return String.Format(“( 0:E, 1:E, 2:E )“, x, y, z); case “IJK“: StringBuilder sb = new StringBuilder(x.ToString(), 30); sb.AppendFormat(“ i + “); sb.AppendFormat(y.ToString(); sb.AppendFormat(“ j + “); sb.AppendFormat(z.ToString(); sb.AppendFormat(“ k“); return sb.ToString(); default: return ToString(); ,5. Vector,public override string ToString() return “( ” + x + “, ” + y + “, ” + z + “ )”; public double Norm() return x*x + y*y + z*z; static void Main() Vector v1 = new Vector(1,32,5); Vector v2 = new Vector(845.4, 54.3, -7.8); Console.WriteLine(“nIn IJK format,nv1 is 0,30:IJKnv2 is 1,30:IJK“, v1, v2); Console.WriteLine(“nIn default format,nv1 is 0,30nv2 is 1,30“, v1, v2); ,In IJK format, v1 is 1 i + 32 j + 5 k v2 i
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年邵阳辅警招聘考试题库及完整答案详解一套
- 2025年甘南州辅警招聘考试题库及答案详解(新)
- 2025年衡水辅警协警招聘考试真题及一套参考答案详解
- 2025年金昌辅警招聘考试题库附答案详解(满分必刷)
- 2025年漳州辅警协警招聘考试备考题库及1套完整答案详解
- 2025年襄樊辅警招聘考试题库含答案详解(培优b卷)
- 2025年黄冈辅警协警招聘考试真题参考答案详解
- 2025年驻马店辅警协警招聘考试备考题库参考答案详解
- 2025年玉林辅警协警招聘考试真题附答案详解(能力提升)
- 2025年那曲辅警招聘考试题库附答案详解(巩固)
- ANSI∕NEMA2502020EnclosuresElectricalEquipment1000VoltsMaximum(电气设备外壳(最大1000伏))
- 2025年上海应用技术大学c语言试题及答案
- DB42-T 2391-2025 全域国土综合整治项目实施方案编制指南
- 无讼学院实习律师培训结业考试题目含答案
- DG-TJ08-2021-2025 干混砌筑砂浆抗压强度现场检测技术标准
- 养老院护理员培训课件
- 关于畜禽交易管理办法
- 神经内科眩晕病例讨论课件
- 闲置设备设施管理办法
- 青春奋斗主题班会课件
- 高压氧科治疗技术应用指南
评论
0/150
提交评论