Text Size

STRINGS IN C#

PDF 

Tutorials

(1 vote, average: 5.00 out of 5)

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.

Trackback(0)
Comments (0)add comment

Write comment

busy

Site Statistics

Stats
Total Members
: 542
Total Discussion
: 0
Total Albums
: 0
Total Photos
: 0
Total Bulletins
: 0
Total Activities
: 1
Total Wall Posts
: 1