版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、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
2、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
3、 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, IndexO
4、fAny, 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 obje
5、ct, 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 greetingT
6、ext += 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.Str
7、ingBuilder,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 bloc
8、k 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-) cha
9、r 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.
10、 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(strin
11、g 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. Howev
12、er, 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 Str
13、ingBuilder.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.AppendForm
14、at() 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
15、 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
16、. 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
17、 = 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.Append
18、Format( 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 is
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年长沙市儿童福利院公开招聘编外合同制人员招聘备考题库及答案详解(考点梳理)
- 怀化市部分市直事业单位2025年下半年集中公开招聘、公开选调工作人员备考题库及答案详解(新)
- 2026年吉林省辽源市单招职业倾向性考试题库带答案详解(夺分金卷)
- 2026年吉林工程职业学院单招职业适应性测试题库带答案详解(达标题)
- 2026年吉林省经济管理干部学院单招职业适应性考试题库附参考答案详解(完整版)
- 2025年九江市融资担保集团有限公司招聘备考题库有答案详解
- 2026年四川三河职业学院单招职业倾向性考试题库及答案详解(真题汇编)
- 2025年保定安国市兴华中学教师招聘18人备考题库及答案详解(新)
- 湖北省妇幼保健院2026年度招聘79人备考题库带答案详解
- 2025年辽宁理工职业大学招聘备考题库及答案详解参考
- 大学英语语法重点总结
- 涉融资性贸易案件审判白皮书(2020-2024)-上海二中院
- 幼儿园故事课件:《罗卡想要一顶帽子》
- 兽医公共卫生学课件
- 个人借条电子版模板
- GB/T 36507-2023工业车辆使用、操作与维护安全规范
- 茶叶中提取咖啡因
- JJF 1427-2013微机电(MEMS)线加速度计校准规范
- GB/T 8804.2-2003热塑性塑料管材拉伸性能测定第2部分:硬聚氯乙烯(PVC-U)、氯化聚氯乙烯(PVC-C)和高抗冲聚氯乙烯(PVC-HI)管材
- GB/T 7583-1987声学纯音气导听阈测定听力保护用
- GB/T 29086-2012钢丝绳安全使用和维护
评论
0/150
提交评论