.NET Type System, Part II - C# Strings
(Page 6 of 7 )
C# lists the string data type as primitive, although it's reference type, not value type as you may think. In this section we will look at C# strings and some of their related operations.
You initialize a string variable in the same way as you initialize a variable of any other primitive data type, like the following:
int x = 34;
string name = "Michael Youssef";
Note that int and string both are C# keywords, and they alias the .NET Framework Classes. In the case of the int keyword, it's an alias to the structure System.Int32, and for the string keyword it's an alias to the class System.String, so both the keyword and the class refer to a string. The System.String class provides us with methods for replacing, adding and inserting characters, and we will see an example soon. You can't use the new operator to create a string object like the following:
string name = new string("Michael Youssef");
This generates an error.
String literals are included in double quotation marks, and C# has the same escape sequence characters as in C and C++, so slashes must be preceded with another slash as in the following example:
string path = "c:\\My Documents";
You can use the @ operator (as in C and C++) to tell the C# compiler that you don't want escape sequence processing in this string:
string path = @"c:\My Documents";
Strings in C# can't be changed once created -- it's immutable -- so the following code simply returns a new string instance and stores it in the variable; it's not the same object:
string test = "Michael";
test += " Youssef";
You can concatenate strings using C# + operator:
string name = "Michael" + " " + "Youssef";
or the following:
string firstName = "Michael ";
string lastName = "Youssef";
string name = firstName + lastName;
There's an interesting issue you should know at this point, which is the difference between concatenating the literal strings and concatenating string variables. When you concatenate literal strings the C# compiler evaluates the expression and ends up with the full concatenated string that it placed as the metadata. When you concatenate variable strings, the C# compiler will evaluate the expression at runtime. If you use several string variables, don't use the + operator to concatenate strings, because it creates multiple string objects on the Managed Heap which will slow the process. Instead, use the class System.Text.StringBuilder. Let's look at some example of using strings in our applications.
Suppose that we have a delimited text file, or a string to make it simpler, and we need to remove the comma and return the text. System.String class provides us with a static method called Split which takes an array of type Char (Unicode Character) which will be used to substring the text and return an array of strings based on this Char array. In our case this array contains only the ',' Char, and it will return the names in a string array after splitting the text, so let's get to the code:
using System;
namespace Arrays
{
class SingleDimension
{
static void Main(string[] args)
{
string delimitedText = "Michael,Youssef,John,Gary,Gerry";
Char[] chars = new char[1] {','};
string[] names = delimitedText.Split(chars);
foreach(string x in names)
{
Console.WriteLine(x);
}
Console.ReadLine();
}
}
}
The result:
Michael
Youssef
John
Gary
Gerry
Sometimes you will need to replace strings, for example you might need to replace all occurrences of "Mick" with "Michael." Let's see how to do that:
using System;
namespace Arrays
{
class SingleDimension
{
static void Main(string[] args)
{
string base = "Hey Mick, how are you Mick?";
string replacement = "Michael";
base = base.Replace("Mick",replacement);
Console.WriteLine(base);
Console.ReadLine();
}
}
}
the result is:
Hey Michael, how are you Michael?
As you can see, the code creates two strings. One is the replacement string and the other is the base string. The Replace method simply replaces the string and returns a new object, so we need to assign the new object to the variable base again. All this is possible because, when you create a string, it derives from System.String, which provides many methods as you saw in the previous examples.
The System.String class contains a method called Substring, which actually returns a substring of a given string. Let's write some code using it:
using System;
namespace Arrays
{
class SingleDimension
{
static void Main(string[] args)
{
string x = "the car is BMW";
string car = x.Substring(11,3);
Console.WriteLine(car);
Console.ReadLine();
}
}
}
the result is
BMW
The Substring method takes two integer values: the first indicates the starting position, which is zero based, and the second tells how many characters to return to the new string.
Next: Boxing Operation >>
More .NET Articles
More By Michael Youssef