Chapter12 - Working with String potx

52 237 0
Chapter12 - Working with String potx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chapter 12. Working With String Hoang Anh Viet VietHA@it-hut.edu.vn HaNoi University of Technology 1 MicrosoftMicrosoft Objectives 2 “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 Framework doesn’t already know about.” MicrosoftMicrosoft 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. . 3 MicrosoftMicrosoft 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. 4 MicrosoftMicrosoft 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. 5 MicrosoftMicrosoft Alias and String Class 6  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 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"; 7 // 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 = "I'm 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 can't 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); Declaring and Initializing Strings 8 MicrosoftMicrosoft 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. 9 MicrosoftMicrosoft 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: Hello 10 [...]... unicodeString); Console.WriteLine("Ascii converted string: {0}", asciiString); Console.Read(); } } } 33 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 Microsoft 34 12.5 StringBuilder  The System.Text.StringBuilder class can be used to modify a string without... 23 Object.ToString, IFormattable  All built-in numeric types as well as date-time types implement this interface  An object that implements the IFormatProvider interface is— surprise—a format provider Microsoft 24 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... 12.4 Working String from Outsite Sources 12 5 StringBuilder 12.6 Searching Strings with Regular Expression Microsoft 12 12.2 String Literals  Use regular string literals when we must embed escape characters provided by C#  For example: string columns = "Column 1\tColumn 2\tColumn 3"; //Output: Column 1 Column 2 Column 3 string rows = "Row 1\r\nRow 2\r\nRow 3"; /* Output: Row 1 Row 2 Row 3 */ string. .. Object.ToString Suports one of the custom format strings 20 Example class FormatString { static void Main() { // Get user input System.Console.WriteLine("Enter a number"); string input = System.Console.ReadLine(); // Convert the input string to an int int j; System.Int32.TryParse(input, out j); // Write a different string each iteration string s; for (int i = 0; i < 10; i++) { // A simple format string with. .. Searching Strings with Regular Expression Microsoft 25 12.4 Working String from Outside Sources   Reason: • We need to interface with the outside world using some other form of encoding, such as UTF-8 • We may use big-endian Unicode strings or little-endian Unicode strings (Intel platform) The NET Framework makes this conversion work easy with the System.Text.Encoding class [SerializableAttribute]... useful  Thus, if we call ToString on a System.Int32,we’ll get a string representation of the value within Microsoft 22 Object.ToString, IFormattable  But we want the string representation in hexadecimal format  In fact, there is a way to get a string representation of an object by implementing the IFormattable interface public interface IFormattable { string ToString( string format, IFormatProvider... Vertical tab 0x000B Microsoft 16 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 Microsoft 17 12.3 Format Specifiers and Globalization   Format the data to display to users in a specific way • Dealing with these sorts of issues • Handling formatting... a new object  Why? • To decrease to store strings in the memory after performing repeated modifications to a string • To boost performance when concatenating many strings together in a loop  We can create a new instance of the StringBuilder class by initializing a variable with one of the overloaded constructor methods StringBuilder MyStringBuilder = new StringBuilder("Hello World!"); Microsoft 35... Microsoft 27 The Standard Unicode UTF-x Describle UTF-8 Which represents each code point as a sequence of one to four bytes UTF-16 Which represents each code point as a sequence of one to two 16-bit integers UTF-32 Which represents each code point as a 32-bit integer Microsoft 28 Unicode encodings Kinds of Encodings Describle ASCIIEncoding Encodes Unicode characters as single 7-bit ASCII characters UTF7Encoding... 13 Regular and Verbatim String Literals  Use verbatim strings for convenience and better readability when the string text contains backslash characters, for example in file paths  Verbatim strings use the delaration preceded with the @ character  For example: Microsoft 14 Example string filePath = @"C:\Users\scoleridge\Documents\"; //Output: C:\Users\scoleridge\Documents\ string text = @"My pensive . and String Class 6  Alias • In C#, the string keyword is an alias for String -& gt; string and String are equivalent.  String Class • The String class provides many methods for  Creating strings  Manipulating. 3 MicrosoftMicrosoft 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. 4 MicrosoftMicrosoft String. about.” MicrosoftMicrosoft 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. .

Ngày đăng: 02/08/2014, 09:20

Mục lục

  • Chapter 12. Working With String

  • Objectives

  • Roadmap

  • 12.1 String Overview

  • String Basics

  • Alias and String Class

  • Declaring and Initializing Strings

  • Declaring and Initializing Strings

  • Immutability of String Objects

  • Immutability of String Objects

  • Remark

  • Slide 12

  • 12.2 String Literals

  • Regular and Verbatim String Literals

  • Example

  • String Escape Sequences

  • Slide 17

  • 12.3 Format Specifiers and Globalization

  • Format Strings

  • Format Strings

Tài liệu cùng người dùng

Tài liệu liên quan