C#,ch9字符串和正则表达式

上传人:新** 文档编号:588864629 上传时间:2024-09-09 格式:PPT 页数:19 大小:757KB
返回 下载 相关 举报
C#,ch9字符串和正则表达式_第1页
第1页 / 共19页
C#,ch9字符串和正则表达式_第2页
第2页 / 共19页
C#,ch9字符串和正则表达式_第3页
第3页 / 共19页
C#,ch9字符串和正则表达式_第4页
第4页 / 共19页
C#,ch9字符串和正则表达式_第5页
第5页 / 共19页
点击查看更多>>
资源描述

《C#,ch9字符串和正则表达式》由会员分享,可在线阅读,更多相关《C#,ch9字符串和正则表达式(19页珍藏版)》请在金锄头文库上搜索。

1、JILIN UNIVERSITYCollege of Computer Science & Technology9 Strings and Regular ExpressionsShuai L2012.9.52/190. IntroductionThe string keyword in C# actually refers to the System.String .NET base class.System.TextSystem.Text.StringBuilderIFormatProvider, IFormattableSystem.Text.RegularExpressions3/19

2、1. System.StringSystem.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 overloadsstring message1 = Hello; / returns Hellomessage1 += , There; / returns Hello, Therestring message2 = message1 + !;

3、 / returns Hello, There!C# also allows extraction of a particular character using an indexer-like syntaxstring message = Hello;char char4 = message4; / returns o. Note the string is zero-indexed4/191. System.StringThe key methods of System.StringCompare, CompareOrdinalConcat, CopyTo, Insert, Join, R

4、eplace, Split, SubstringFormatIndexOf, IndexOfAny, LastIndexOf, LastIndexOfAnyPadLeft, PadRightToLower, ToUpper, Trim5/191. System.StringThe 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

5、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.6/191. System.Stringstring greetingText = Hello from a

6、ll the guys at Wrox Press. ; /39 chsgreetingText += We do hope you enjoy this book as much as we enjoyed writing it.; / 39+65-1=103 chsfor(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(Encode

7、d:n + greetingText);7/192. System.Text.StringBuilderThe StringBuilder normally allocates more memory than is actually needed.Lengthwhich indicates the length of the string that it actually containsCapacitywhich indicates the maximum length of the string in the memory allocationAny modifications to t

8、he string take place within the block of memory assigned to the StringBuilder instance.8/192. System.Text.StringBuilderStringBuilder 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 writ

9、ing 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);9/192. System.Text.StringBuilderThe key methods of System.Text.StringBuilderAppend, AppendFormatInser

10、t, Remove, ReplaceToString10/193. Format StringsConsole.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 thi

11、s:public void WriteLine(string format, object arg0, object arg1)this.WriteLine(string.Format(this.FormatProvider, format, new objectarg0, arg1);11/193. Format StringsString.Format() now needs to construct the final string by replacing each format specifier with a suitable string representation of th

12、e 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.12/193. Format StringsIn this example, a StringBuilder instance is created and initialized with the first known portion of the string, the text

13、 “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()

14、 and StringBuilder.AppendFormat() being called repeatedly until the entire formatted string has been obtained.13/193. Format StringsStringBuilder.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 namespa

15、ce 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

16、override. 14/193. Format Strings15/194. IFormattable Interfaceinterface IFormattablestring ToString(string format, IFormatProvider formatProvider);16/195. Vectorstruct Vector: IFormattablepublic double x, y, z;public string ToString(string format, IFormatProvider formatProvider)if (format = null)ret

17、urn ToString();string formatUpper = format.ToUpper();switch (formatUpper)17/195. Vectorswitch (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.AppendF

18、ormat(y.ToString();sb.AppendFormat( j + );sb.AppendFormat(z.ToString();sb.AppendFormat( k);return sb.ToString();default:return ToString();18/195. Vectorpublic override string ToString() return “( ” + x + “, ” + y + “, ” + z + “ )”; public double Norm() return x*x + y*y + z*z; static void Main()Vecto

19、r 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 kv2 is 845.4 i + 54.3 j + -7.8 kIn default format,v1 is

20、( 1, 32, 5 )v2 is ( 845.4, 54.3, -7.8 )19/195. Vectorpublic 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 VE formatnv1 is 0,30:VEnv2 is 1,30:VE,v1, v2);Console.WriteLine(nNorms are:nv1 is 0,20:Nnv2 is 1,20:N, v1, v2);In VE formatv1 is ( 1.000000E+000, 3.200000E+001, 5.000000E+000 )v2 is ( 8.454000E+002, 5.430000E+001, -7.800000E+000 )Norms are:v1 is | 1050 |v2 is | 717710.49 |

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号