




已閱讀5頁,還剩14頁未讀, 繼續(xù)免費閱讀
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 我長大了健康領
- 城市管理信息系統(tǒng)
- 寫景朗讀指導課件
- 教育研究方法文獻
- 企業(yè)數(shù)據(jù)架構之數(shù)據(jù)質量提升與企業(yè)級數(shù)據(jù)模型構建策略
- 2025年燒結釹鐵硼永磁材料項目規(guī)劃申請報告
- 2025年家用家具項目立項申請報告模板
- 2025年米面類項目提案報告
- 詞的教學課件
- 五分鐘英文教學試講課件
- 危重癥鎮(zhèn)痛鎮(zhèn)靜的護理
- 2025年爐外精煉工職業(yè)技能考試題庫及答案(完整版)
- TCRHA 088-2024 病理免疫組織化學檢測質控品要求
- 2025年高考語文備考之文學類文本閱讀:敘述視角專項限時練
- 起重機司機Q2(限橋式起重機)題庫題庫(1727道)
- 第九章 西半球的國家 單元教學設計-2023-2024學年七年級地理下學期人教版
- 云南錫業(yè)職業(yè)技術學院《影視劇配音》2023-2024學年第一學期期末試卷
- 2025年遼寧沈陽地鐵集團有限公司招聘筆試參考題庫含答案解析
- 體檢中心接待規(guī)范流程
- 2025年全國水務集團招聘筆試參考題庫含答案解析
- 阿細跳月課件
評論
0/150
提交評論