Title: INT422 Internet III Web Programming on Windows
1INT422 Internet III Web Programming on
Windows
- .NET Framework classes
- String and StringBuilder
2.NET Framework BCL examplesString and
StringBuilder
3Strings in .NET
- The String Class is the only primitive type in
.NET that is a Reference Type - Strings are immutable (i.e., once created, you
cant change their value) - NOTE Strings are NOT arrays of Chars. The Char
type is a Unicode character. - Using Strings is actually faster than Chars
- The CLR converts Chars to Strings when compiling
4Stringmore
- System.String has 92 separate method signatures,
2 properties, and 1 constant! - Learning to use String efficiently is important,
since so much of web programming uses text - Methods you should learn
- StartsWith() EndsWith() IndexOf() LastIndexOf()
- Remove() Replace() Split() SubString()
- ToLower() ToUpper() ToString(format) Trim()
5Examples of string methods (1)
- String s "INT422 - Windows Web Programming"
- Console.WriteLine(s.StartsWith("INT422"))
- // StartsWith(search string) -gt True
- Console.WriteLine(s.EndsWith("INT422"))
- // EndsWith(search string) -gt False
- Console.WriteLine(s.IndexOf("-"))
- // IndexOf(string to search for) -gt 7
- // (i.e., pos from 0 of "-")
- Console.WriteLine(s.LastIndexOf("i"))
- // LastIndexOf(string to search for) -gt 29
- // (i.e.,last "i")
- Console.WriteLine(s.Remove(0, 9))
- // Remove(start, count) -gt Windows Web
Programming - Console.WriteLine(s.Replace("Windows", ".NET"))
- // Replace(old, new) -gt INT422 - .NET Web
Programming
6Examples of string methods (2)
- String words s.Split()
- // Splits string at spaces,
- // giving array with each separate word
- Console.WriteLine(s.Substring(9))
- // Substring(start) -gt Windows Web Programming
- Console.WriteLine(s.Substring(9, 7))
- // Substring(start, length) -gt Windows
- Console.WriteLine(s.ToUpper())
- // INT422 - WINDOWS WEB PROGRAMMING
- Console.WriteLine(s.ToLower())
- // int422 - windows web programming
- Console.WriteLine(" INT422 ".Trim())
- // removes leading trailing spaces -gt "INT422"
7String Inefficiency Exposed
string s1 Hello string s2 World
Unused Managed Memory
Reference (s2)
String (World)
Reference (s1)
String (Hello)
...
...
Stack
Managed Heap
8String Inefficiencymore
s1 s1 s2
Unused Managed Memory
String still exists on the heap until Garbage
Collected
Reference (s2)
String (Hello World)
String (World)
Reference (s1)
String (Hello)
...
...
s1 Reference now refers to a new String object
Stack
Managed Heap
9A Better String - StringBuilder
- Since Strings are immutable, they arent
efficient if you need to concatenate a lot of
text - The System.Text.StringBuilder class was designed
to solve this problem - StringBuilder allows you to Append text without
having to always create new strings - The memory is expanded dynamically when needed
- Once youre done building the text, use the
ToString() method to get a String object
10Anatomy of a StringBuilder
System.Text.StringBuilder sb sb new
System.Text.StringBuilder(7) sb.Append(Hello)
Capacity
H
e
l
l
o
Text is added to end and more Capacity added if
necessary dynamically
Length
sb.Append(Bye)
H
e
l
l
o
B
y
e
11Using the StringBuilder
- Imports System.Text
- // The capacity could be anything 0 or 10 or
1024... - StringBuilder sb new StringBuilder(1024)
- sb.Append("lthtmlgtltheadgtlttitlegtStringBuilder
Sitelt/titlegtlt/headgt") - // Inserting a NewLine character--not inserted by
default - sb.Append(ControlChars.NewLine)
- sb.Append("ltbodygtlth1gtStringBuilderlt/h1gtltbrgtltbrgt")
- sb.Append("ltpgtThe StringBuilder is a better
String.lt/pgt") - sb.Append("lt/bodygtlt/htmlgt")
- // To get a String out of the StringBuilder, use
ToString() - string s sb.ToString()
- Console.WriteLine(s)
- // Capacity vs. Length
- Console.WriteLine("Capacity " sb.Capacity)
- Console.WriteLine("Length " sb.Length)
Output