Strings consist of sequences of characters contained in string object. A string object may be created using a number of different mechanisms.
+
Strings consist of sequences of characters contained in a string object. A string object may be created using a number of different mechanisms.
A string may be declared but not initialized as follows:
A string may be declared but not initialized as follows:
</pre>
</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:
+
String literals are placed within double quotes (as shown above). If the string itself contains double quotes the escape character (\) should precede the double quote characters:
<pre>
<pre>
−
System.Console.WriteLine ("He shouted \"Can you here me?\"");
+
System.Console.WriteLine ("He shouted \"Can you hear me?\"");
</pre>
</pre>
== Obtaining the Length of a C# String ==
== Obtaining the Length of a C# String ==
−
The length of a C# may be obtained by accessing the ''Length'' property of the string object:
+
The length of a C# string may be obtained by accessing the ''Length'' property of the string object:
<pre>
<pre>
== Treating Strings as Arrays ==
== 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]].
+
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:
+
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: