String Objectives Discuss string handling System.String class - - PDF document

string objectives
SMART_READER_LITE
LIVE PREVIEW

String Objectives Discuss string handling System.String class - - PDF document

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


slide-1
SLIDE 1

String

slide-2
SLIDE 2

Objectives

  • Discuss string handling

– System.String class – System.Text.StringBuilder class

2

slide-3
SLIDE 3

String class

  • Framework Class Library provides System.String class

– represents immutable sequence of characters

namespace System { public sealed class String ... { ... } }

String class

3

slide-4
SLIDE 4

String alias

  • C# provides string as convenient alias for System.String

string s; System.String t;

equivalent

4

slide-5
SLIDE 5

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' }; string a = new string(t); string b = new string(t, 0, 2); string c = new string('z', 3); entire array first 2 characters given character repeated 3 times

a hello b he c zzz

5

slide-6
SLIDE 6

String literals

  • String literals automatically converted into string objects

– convenient shorthand for common case

string s = "hello";

string object created

s hello

6

slide-7
SLIDE 7

Escape sequence

  • Can include escape sequence in string

– precede special characters with \

string a = "hello\n"; string b = "\""; string c = "C:\\WINDOWS"; ...

linefeed double quote backslash

7

slide-8
SLIDE 8

Verbatim string literal

  • Can create verbatim string literal

– precede with @ – helps reduce need for escape sequences

8

string path = @"C:\WINDOWS\Config";

verbatim

slide-9
SLIDE 9

String length

  • String provides read only Length property

string s = "hello"; int l = s.Length;

length will be 5

9

slide-10
SLIDE 10

String indexer

  • String provides read only indexer

– indices start at zero

string s = "hello"; char c = s[4];

retrieve 'o'

10

slide-11
SLIDE 11

String comparison

  • String provides several ways to perform comparison

– all have value semantics

string a = "bike"; string b = "bit"; if (a.CompareTo(b) < 0) ... if (a.Equals(b)) ... if (a == b) ... if (a != b) ...

three way comparison, used for ordering equality equality inequality

11

slide-12
SLIDE 12

String concatenation

  • String concatenation supported

– operator + – operator +=

string a = "holly"; string b = "wood"; string c = a + b;

  • perator +

string d = "tom"; d += "cat";

  • perator +=

12

slide-13
SLIDE 13

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"; d += "cat";

create new object, assign to reference

tom d tomcat

13

slide-14
SLIDE 14

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++) { text += Console.ReadLine(); }

may create new

  • bject each iteration

14

slide-15
SLIDE 15

StringBuilder class

  • Framework Class Library provides StringBuilder class

– in System.Text namespace – represents mutable sequence of characters

namespace System.Text { public sealed class StringBuilder { ... } }

StringBuilder class

15

slide-16
SLIDE 16

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("<body>hello<html>"); text.Remove(12, 4); text.Insert(12, "/bode"); text[16] = 'y';

modify

text <body>hello</body>

16

slide-17
SLIDE 17

StringBuilder internally managed capacity

  • StringBuilder adjusts capacity to accommodate contents

– no need for user interaction

StringBuilder text = new StringBuilder(); text.Append("<html>"); text.Append("<body>"); text.Append("<p>"); text.Append("hello"); ... text.Append("</p>"); text.Append("</body>"); text.Append("</html>");

capacity increased as needed

17

slide-18
SLIDE 18

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

18

StringBuilder a = new StringBuilder(100); a.Capacity = 200;

capacity of 100 capacity of 200

slide-19
SLIDE 19

StringBuilder typical usage

  • StringBuilder typically used to create desired sequence

– result is then converted to string using ToString

string Create() { StringBuilder text = new StringBuilder(); text.Append("<html>"); text.Append("<body>"); text.Append("<p>"); text.Append("hello"); ... return text.ToString(); }

create convert

19

slide-20
SLIDE 20

Summary

20

  • .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