C#,ch9.字符串和正則表達(dá)式.ppt_第1頁(yè)
C#,ch9.字符串和正則表達(dá)式.ppt_第2頁(yè)
C#,ch9.字符串和正則表達(dá)式.ppt_第3頁(yè)
C#,ch9.字符串和正則表達(dá)式.ppt_第4頁(yè)
C#,ch9.字符串和正則表達(dá)式.ppt_第5頁(yè)
已閱讀5頁(yè),還剩14頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論