Tutorials
Desktop Programming
C Sharp
STRINGS IN C#
Written by Christopher Thursday, 29 May 2008 15:53
Tutorials
INTRODUCTION
The string class is designed specifically for storing and manipulating strings. Some operations of this class include conversion to lower or uppercase, removal of leading and trailing whitespace, replacing characters, comparing two strings and more. Also, a ToString() method is inherited to every object in C#, making available a string conversion of any object. Of course, this method can be overridden in your classes so that you can define the process in which this conversion takes place.
USING STRINGS
A string can be defined can be used just like any other variable in C#
string myString = "Hello World";
Suppose that you want to change this string to represent another text value, for example:
myString = "GoodBye World";
In all variable types of C# the value of the object is changed to reflect the new one. In a string object however, this is not possible. Instead, the old object is destoyed, a new one is created and it is assigned the new value. The same is true for string methods, such as all methods and operators that need to modify the contents of a string object:
string myString = "Hello ";
string myOtherString = "World";
myString += myOtherString;
In the above example two strings are defined and initialized. The .Net framework allocates space sufficient to hold this data, but not more. Next, a new string is created with enough capacity to hold the joined text. This text is then assigned to the new object and the old ones are unreferenced, and subject to removal from the Garbage collector. If you need to make numerous changes in your strings, your program will suffer performance losses due to this fact. Therefore, C# provides another class, the streambuilder class that is more effective in terms of memory management, but limited to substitution and appending or removing text. The programmer can define how much memory is assigned to each stringbuilder object by setting the capacity property to a value bigger than the capacity required by the actual text. The property Length indicates the length of the string that is actually contained in the stringbuilder object. If you try to set the capacity of a stringbuilder object to a value less than the length of the current string an exception will be raised. You can also convert a stringbuilder object to a string by using the well-known ToString() method. Follows an example of a stringbuilder object definition and usage:
StringBuilder myBuilder = new StringBuilder("Hello World");
Console.WriteLine(myBuilder.Capacity);
Console.WriteLine(myBuilder.Length);
myBuilder.Append(" Again ");
Console.WriteLine(myBuilder.Capacity);
Console.WriteLine(myBuilder.Length);
Console.ReadLine();
When the stringbuilder object is created the .Net allocates a certain capacity for it. When you add more text that requires more capacity, the .Net allocates more space for this object, usually by doubling the previous capacity.

| < Prev | Next > |
|---|
| Training Tip #1 :: Push-ups on the Swiss Ball admin 28.4.2009 7:07 |
Re:hello admin 21.4.2008 14:11 |
| Dell Inspiron 1545 Windows XP Drivers admin 12.4.2009 8:03 |
hello yuppy 21.4.2008 10:28 |
| Re:Get rich ebooks - are they really working? lann 23.4.2008 17:04 |
Re:Get rich ebooks - are they really working? admin 19.4.2008 14:08 |