Chapter12 - Working with String
Số trang: 52
Loại file: ppt
Dung lượng: 511.00 KB
Lượt xem: 15
Lượt tải: 0
Xem trước 6 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
“Describes how strings are a first-class type in the CLR and howto use them effectively in C#. A large portion of the chaptercovers the string-formatting capabilities of various types in the.NET Framework and how to make your defined types behavesimilarly by implementing IFormattable. Additionally, I introduceyou to the globalization capabilities of the framework and how tocreate custom CultureInfo for cultures and regions that the .NETFramework doesn’t already know about.”
Nội dung trích xuất từ tài liệu:
Chapter12 - Working with StringChapter 12. Working With String Hoang Anh Viet VietHA@it-hut.edu.vn HaNoi University of Technology 1 Objectives “Describes how strings are a first-class type in the CLR and how to use them effectively in C#. A large portion of the chapter covers the string-formatting capabilities of various types in the .NET Framework and how to make your defined types behave similarly by implementing IFormattable. Additionally, I introduce you to the globalization capabilities of the framework and how to create custom CultureInfo for cultures and regions that the .NET doesn’t Framework already know about.”MicrosoftMicrosoft 2 Roadmap 12 .1 String Overview 12.2 String Literals 12.3 Format Specifiers and Globalization 12.4 Working String from Outsite Sources 12 5 StringBuilder 12.6 Searching Strings with Regular Expression. .MicrosoftMicrosoft 3 12.1 String Overview In C#, String is a built-in type. In the built-in type collection , String is a reference type and but most of the built-in types are value types.MicrosoftMicrosoft 4 String Basics A string is an object of type String whose value is text. The text is stored as a readonly collection of Char objects. Each of which represents one Unicode character encoded in UTF-16. There is no null-terminating character at the end of a C# string (unlike C and C++). therefore a C# string can contain any number of embedded null characters (\0). The length of a string represents the number of characters regardless of whether the characters are formed from Unicode surrogate pairs or not.MicrosoftMicrosoft 5 Alias and String Class Alias • In C#, the string keyword is an alias for String -> string and String are equivalent. String Class • The String class provides many methods for Creating strings Manipulating strings Comparing strings. • It overloads some operators to simplify common string operations.MicrosoftMicrosoft 6 Declaring and Initializing Strings We can declare and initialize strings in various ways, as shown in the following example: // Declare without initializing. string message1; // Initialize to null. string message2 = null; // Initialize as an empty string. // Use the Empty constant instead of the literal . string message3 = System.String.Empty; //Initialize with a regular string literal. string oldPath = c:\\Program Files\\Microsoft Visual Studio 8.0;MicrosoftMicrosoft 7Declaring and Initializing Strings// Use System.String if we prefer.System.String greeting = Hello World!; // In local variables (i.e. within a method body)// you can use implicit typing.var temp = Im still a strongly-typed System.String!;// Use a const string to prevent message4 from // being used to store another string value.const string message4 = You cant get rid of me!;// Use the String constructor only when creating// a string from a char*, char[], or sbyte*. See// System.String documentation for details.char[] letters = { A, B, C };string alphabet = new string(letters); 8 Immutability of String Objects String objects are immutable: they cannot be changed after they have been created. All of the String methods and C# operators that appear to modify a string actually return the results in a new string object. For example: string s1 = A string is more ; string s2 = than the sum of its chars.; // Concatenate s1 and s2. This actually creates a new // string object and stores it in s1, releasing the // reference to the original object. s1 += s2; System.Console.WriteLine(s1); // Output: A string is more than the sum of its chars.MicrosoftMicrosoft 9 Immutability of String Objects Note: • When create a reference to a string, and then modify the original string, the reference will continue to point to the original object instead of the new object that was created when the string was modified. For example: string s1 = Hello ; string s2 = s1; s1 += World; System.Console.WriteLine(s2); //Output: HelloMicrosoftMicrosoft 10 Remark When we declare a string in your C# code, the compiler creates a System.String object for us. And then it places into an internal table in the module called the intern pool. The compiler first checks to see if we’ve declared the same string elsewhere, and if we have, then the code simply references the one already interned.MicrosoftMicrosoft 11 Roadmap 12 .1 String Overview 12.2 String Literals 12.3 Format Specifiers and Globalization 12.4 Working String from Outsite Sources 12 5 StringBuilder 12.6 Searching Strings with Regular Expression. .MicrosoftMicrosoft 12 ...
Nội dung trích xuất từ tài liệu:
Chapter12 - Working with StringChapter 12. Working With String Hoang Anh Viet VietHA@it-hut.edu.vn HaNoi University of Technology 1 Objectives “Describes how strings are a first-class type in the CLR and how to use them effectively in C#. A large portion of the chapter covers the string-formatting capabilities of various types in the .NET Framework and how to make your defined types behave similarly by implementing IFormattable. Additionally, I introduce you to the globalization capabilities of the framework and how to create custom CultureInfo for cultures and regions that the .NET doesn’t Framework already know about.”MicrosoftMicrosoft 2 Roadmap 12 .1 String Overview 12.2 String Literals 12.3 Format Specifiers and Globalization 12.4 Working String from Outsite Sources 12 5 StringBuilder 12.6 Searching Strings with Regular Expression. .MicrosoftMicrosoft 3 12.1 String Overview In C#, String is a built-in type. In the built-in type collection , String is a reference type and but most of the built-in types are value types.MicrosoftMicrosoft 4 String Basics A string is an object of type String whose value is text. The text is stored as a readonly collection of Char objects. Each of which represents one Unicode character encoded in UTF-16. There is no null-terminating character at the end of a C# string (unlike C and C++). therefore a C# string can contain any number of embedded null characters (\0). The length of a string represents the number of characters regardless of whether the characters are formed from Unicode surrogate pairs or not.MicrosoftMicrosoft 5 Alias and String Class Alias • In C#, the string keyword is an alias for String -> string and String are equivalent. String Class • The String class provides many methods for Creating strings Manipulating strings Comparing strings. • It overloads some operators to simplify common string operations.MicrosoftMicrosoft 6 Declaring and Initializing Strings We can declare and initialize strings in various ways, as shown in the following example: // Declare without initializing. string message1; // Initialize to null. string message2 = null; // Initialize as an empty string. // Use the Empty constant instead of the literal . string message3 = System.String.Empty; //Initialize with a regular string literal. string oldPath = c:\\Program Files\\Microsoft Visual Studio 8.0;MicrosoftMicrosoft 7Declaring and Initializing Strings// Use System.String if we prefer.System.String greeting = Hello World!; // In local variables (i.e. within a method body)// you can use implicit typing.var temp = Im still a strongly-typed System.String!;// Use a const string to prevent message4 from // being used to store another string value.const string message4 = You cant get rid of me!;// Use the String constructor only when creating// a string from a char*, char[], or sbyte*. See// System.String documentation for details.char[] letters = { A, B, C };string alphabet = new string(letters); 8 Immutability of String Objects String objects are immutable: they cannot be changed after they have been created. All of the String methods and C# operators that appear to modify a string actually return the results in a new string object. For example: string s1 = A string is more ; string s2 = than the sum of its chars.; // Concatenate s1 and s2. This actually creates a new // string object and stores it in s1, releasing the // reference to the original object. s1 += s2; System.Console.WriteLine(s1); // Output: A string is more than the sum of its chars.MicrosoftMicrosoft 9 Immutability of String Objects Note: • When create a reference to a string, and then modify the original string, the reference will continue to point to the original object instead of the new object that was created when the string was modified. For example: string s1 = Hello ; string s2 = s1; s1 += World; System.Console.WriteLine(s2); //Output: HelloMicrosoftMicrosoft 10 Remark When we declare a string in your C# code, the compiler creates a System.String object for us. And then it places into an internal table in the module called the intern pool. The compiler first checks to see if we’ve declared the same string elsewhere, and if we have, then the code simply references the one already interned.MicrosoftMicrosoft 11 Roadmap 12 .1 String Overview 12.2 String Literals 12.3 Format Specifiers and Globalization 12.4 Working String from Outsite Sources 12 5 StringBuilder 12.6 Searching Strings with Regular Expression. .MicrosoftMicrosoft 12 ...
Tìm kiếm theo từ khóa liên quan:
C sharp database lập trình với C ngôn ngữ lập trình C tài liệu về lập trìnhGợi ý tài liệu liên quan:
-
101 trang 193 1 0
-
Hướng dẫn lập trình với Android part 4
5 trang 143 0 0 -
Tìm hiểu về ngôn ngữ lập trình C: Phần 1 - Quách Tuấn Ngọc
211 trang 143 0 0 -
161 trang 126 1 0
-
Giáo trình Vi điều khiển PIC: Phần 1
119 trang 114 0 0 -
Bài giảng Phương pháp lập trình: Chương 9 - GV. Từ Thị Xuân Hiền
36 trang 108 0 0 -
Đồ án vi xử lý đề tài : nghiên cứu thiết kế mạch đo khoảng cách sử dụng vi điều khiển Pic 16F887
45 trang 90 1 0 -
Tìm hiểu về ngôn ngữ lập trình C: Phần 2 - Quách Tuấn Ngọc
210 trang 84 0 0 -
Trắc nghiệm 300 câu phân tích và thiết kế hệ thống
29 trang 81 0 0 -
863 trang 75 0 0