String
lượt xem 1
download
String presents about string class; string alias; string creation; string literals; escape sequence; verbatim string literal; string length; string indexer; string comparison; string concatenation and something else.
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: String
- String
- Objectives • Discuss string handling – System.String class – System.Text.StringBuilder class 2
- String class • Framework Class Library provides System.String class – represents immutable sequence of characters namespace System { String class public sealed class String ... { ... } } 3
- String alias • C# provides string as convenient alias for System.String string s; equivalent System.String t; 4
- String creation • String is a reference type – created with new – constructors available for char and char[] char[] t = new char[5] { 'h', 'e', 'l', 'l', 'o' }; entire array string a = new string(t); first 2 characters string b = new string(t, 0, 2); given character string c = new string('z', 3); repeated 3 times a hello b he c zzz 5
- String literals • String literals automatically converted into string objects – convenient shorthand for common case string object created string s = "hello"; s hello 6
- Escape sequence • Can include escape sequence in string – precede special characters with \ linefeed string a = "hello\n"; double quote string b = "\""; backslash string c = "C:\\WINDOWS"; ... 7
- Verbatim string literal • Can create verbatim string literal – precede with @ – helps reduce need for escape sequences verbatim string path = @"C:\WINDOWS\Config"; 8
- String length • String provides read only Length property string s = "hello"; length will be 5 int l = s.Length; 9
- String indexer • String provides read only indexer – indices start at zero string s = "hello"; retrieve 'o' char c = s[4]; 10
- String comparison • String provides several ways to perform comparison – all have value semantics string a = "bike"; string b = "bit"; three way comparison, if (a.CompareTo(b) < 0) used for ordering ... equality if (a.Equals(b)) ... equality if (a == b) ... inequality if (a != b) ... 11
- String concatenation • String concatenation supported – operator + – operator += string a = "holly"; string b = "wood"; operator + string c = a + b; string d = "tom"; operator += d += "cat"; 12
- String immutability • Strings are immutable – no way to modify contents • Operations which seem to modify string do not – actually create new object to represent new value string d = "tom"; create new object, d += "cat"; assign to reference d tom tomcat 13
- String inefficiency • Strings can be inefficient when used for concatenation – may create and destroy many intermediate objects string text = ""; for (int i = 0; i < 10; i++) { may create new text += Console.ReadLine(); object each iteration } 14
- StringBuilder class • Framework Class Library provides StringBuilder class – in System.Text namespace – represents mutable sequence of characters namespace System.Text { StringBuilder class public sealed class StringBuilder { ... } } 15
- StringBuilder mutability • StringBuilder provides mutator methods – change contents of existing object – can be more efficient than string for concatenation StringBuilder text = new StringBuilder(); text.Append("hello"); text.Remove(12, 4); modify text.Insert(12, "/bode"); text[16] = 'y'; text hello 16
- StringBuilder internally managed capacity • StringBuilder adjusts capacity to accommodate contents – no need for user interaction StringBuilder text = new StringBuilder(); text.Append(""); text.Append(""); text.Append(""); capacity increased text.Append("hello"); as needed ... text.Append(""); text.Append(""); text.Append(""); 17
- StringBuilder user managed capacity • StringBuilder allows user some control over capacity – initial capacity during construction – read/write Capacity property • Explicit management of capacity can be efficient – if final length of string is known ahead of time capacity of 100 StringBuilder a = new StringBuilder(100); capacity of 200 a.Capacity = 200; 18
- StringBuilder typical usage • StringBuilder typically used to create desired sequence – result is then converted to string using ToString string Create() { create StringBuilder text = new StringBuilder(); text.Append(""); text.Append(""); text.Append(""); text.Append("hello"); ... convert return text.ToString(); } 19
- Summary • .NET Framework Class Library has two string classes – String – StringBuilder • String is primary class – offers most services – is most convenient to use • StringBuilder is more specialized – targeted toward creating string from pieces 20
CÓ THỂ BẠN MUỐN DOWNLOAD
-
Bài giảng Collection - String
23 p | 63 | 8
-
Storing Connection strings
6 p | 61 | 7
-
Exporting the Results of a Query as a String
4 p | 82 | 6
-
Chapter : STRING
25 p | 50 | 6
-
Bài giảng Lập trình Windows - Chapter 8 : Strings, Chars
17 p | 67 | 5
-
Manipulating Strings
5 p | 67 | 4
-
Chapter 4 - Pointers and Strings
86 p | 61 | 3
-
Bài giảng Tin đại cương: Bài 10 - Trương Xuân Nam
22 p | 126 | 3
-
Bài giảng Lập trình C# 1 - Chương 7: Strings
4 p | 41 | 3
-
Bài giảng Tin học đại cương: Bài 10 - ThS. Nguyễn Thị Phương Thảo
16 p | 38 | 2
-
Bài giảng Tin đại cương - Bài 8: Bài tập về string và mảng
9 p | 51 | 2
-
Bài giảng Tin học đại cương: Bài 10 - Bùi Thị Thu Cúc
16 p | 80 | 2
-
Lecture Windows programming: Chapter 7 - Châu Thị Bảo Hà
55 p | 52 | 2
-
Chapter 9 String
40 p | 43 | 2
-
Bài giảng Tin đại cương: Bài 8 - Trương Xuân Nam
18 p | 130 | 1
-
Bài giảng Tin đại cương: Bài 11 - Trương Xuân Nam
19 p | 118 | 1
-
Lecture Java programming language: Loop Structures and Strings - Ho Dac Hung
10 p | 24 | 1
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn