Changes

Jump to: navigation, search

Working with Strings in C Sharp

5,640 bytes added, 20:17, 23 January 2008
no edit summary
== Creating Strings in C# ==
 
Strings consist of sequences of characters contained in string object. A string object may be created using a number of different mechanisms.
 
A string may be declared but not initialized as follows:
 
<pre>
string myString;
</pre>
 
A literal value may be assigned to a string in C# using the assignment operator:
 
<pre>
string myString = "Hello World";
</pre>
 
Alternatively a new string may be created using the ''new'' keyword and passing through the literal value to the constructor:
 
<pre>
string myString = new String("Hello World");
</pre>
 
String literals are placed with double quotes (as shown above). If the string itself contains double quotes the escape character (\) should precede the double quote characters:
 
<pre>
System.Console.WriteLine ("He shouted \"Can you here me?\"");
</pre>
 
C# can be instructed to treat all the characters in a string ''verbatim'' using the @ notation. When using the @ notation everything between the double quotes is treated as a raw string, regardless of whether new lines, carriage returns, backslashes etc are present in the text.
 
For example:
 
<pre>
System.Console.WriteLine (@"You can put a backslash \ here
and a new line
and tabs work too.
You can also put in sequences that would normally be seen as escape sequences \n \t");
}
</pre>
 
The above jumble of text will be faithfully reproduced character for character as follows:
 
<pre>
You can put a backslash \ here
and a new line
and tabs work too.
You can also put in sequences that would normally be seen as escape sequences \n \t
</pre>
 
Programmers familiar with the ''heredoc'' function of other programming language will quickly notice that this is essentially the C# equivalent.
 
== Obtaining the Length of a C# String ==
 
The length of a C# may be obtained by accessing the ''Length'' property of the string object:
 
<pre>
String myString = "Hello World";
 
System.Console.WriteLine ("myString length = " + myString.Length);
</pre>
 
When executed, the output will read "myString length = 11".
 
== Treating Strings as Arrays ==
 
It is possible to access individual characters in a string by treating the string as an array (for details on C# arrays read the chapter entitled [[Introducing C Sharp Arrays|Introducing C# Arrays]].
 
By specifying the index into the array of the character individual characters may be accessed. It is important to note that strings are immutable (in other words the value of a string cannot be modified unless an entirely new string literal is assigned to the object). This means that while it is possible to read the value of a character in a string it is not possible to change the value:
 
<pre>
string myString = "Hello World";
 
System.Console.WriteLine(myString[1]); //Displays second character (e)
 
myString[0] = 'h'; // Illegal - string cannot be modified.
</pre>
 
The above code would display the letter e which is the second character of the string (remember that indexes in C# begin at 0). Unfortunately the code would fail to compile because an illegal attempt to change the value of a character is made.
 
== Concatenating Strings in C# ==
 
Strings may be concatenated (i.e joined together) simply by adding them together using the addition operator (+).
 
We can, therefore, combine two strings:
 
<pre>
string myString = "Hello World.";
 
System.Console.WriteLine (myString + " How are you?");
</pre>
 
Resulting in output which reads "Hello World. How are you?".
 
Strings may also be concatenated using the ''Concat()'' method. This method takes two strings to be joined as arguments and returns a third string containing the union of the two strings:
 
<pre>
string myString1 = "Hello World.";
string myString2 = " How are you?";
string myString3;
 
myString3 = String.Concat ( myString1, myString2 );
</pre>
 
== Comparing Strings ==
 
A common mistake when comparing strings is to try to perform the comparison using the equality operator (==). For example:
 
<pre>
String myString1 = "Hello World";
String myString2 = "Hello World";
 
if (myString1 == myString2)
{
System.Console.WriteLine ("They match");
}
else
{
System.Console.WriteLine ("They do not match");
}
</pre>
 
The above example will display the "They do not match" message even though strings contain the same text. This is because the comparison is not comapring the text, but rather comparing the location in memory of one string object with the location of another. Clearly since they are different strings objects the reside at different locations - even though they contain the same text. Comparisons must, therefore, be made using the Compare() method.
 
The C# String Compare() method returns 1 is the left hand string is greater than the right hand string, 0 if the strings match and 1 -1 if the left hand string is less than the right hand string:
 
<pre>
String myString1 = "Hello World";
String myString2 = "Hello World";
 
if (String.Compare (myString1, myString2) == 0)
{
System.Console.WriteLine ("They match");
}
else
{
System.Console.WriteLine ("They do not match");
}
</pre>
 
By default, Compare() performs a case sensitive comparison. If the Compare() is called with a third boolean argument it is possible to control whether the comparison is case sensitive or not. A ''true'' argument tells Compare() to ignore case when performing the comparison:
 
<pre>
String myString1 = "Hello World";
String myString2 = "HELLO WORLD";
 
if (String.Compare (myString1, myString2, true) == 0)
{
System.Console.WriteLine ("They match");
}
else
{
System.Console.WriteLine ("They do not match");
}
</pre>

Navigation menu