The C# string is an object that represents a sequence of characters, each identified by one index.
Creation:
To create strings in C#, you use double quotes like this:
string greeting = "Hello";
Accessing characters:
To access the characters in a string, you use the index operator []
The following example returns the first character of a string with the index zero:
string str = "Hello";
char ch = str[0];
// ch is 'H'
Getting the length:
We can get the length of a string using the Length property.
For example, to access the last character of the string, you use the Length - 1 index:
string str = "Hello";
char ch = str[str.Length - 1];
// ch is 'o'
Concatenating strings via + operator:
To concatenate two or more strings, you use the + operator:
string name = "John";
string str = "Hello " + name;
Console.WriteLine(str); // prints "Hello John"
If you want to assemble a string piece by piece, you can use the += operator:
string greet = "Welcome";
string name = "John";
greet += " to AlgoCademy, ";
greet += name;
Console.WriteLine(greet); // "Welcome to AlgoCademy, John"
When concatenating many strings, it is recommended to use StringBuilder for performance
String slicing
We often want to get a substring of a string. For this, we use the Substring() method.
Substring(startIndex, length) returns the substring that starts at startIndex and has length characters:
string str = "JavaScript";
string substr = str.Substring(2, 4);
Console.WriteLine(substr); // prints "vaSc"
The startIndex is a zero-based index at which the Substring() starts the extraction.
The length is the number of characters to extract.
If you omit the length, the Substring() extracts to the end of the string:
string str = "JavaScript";
string substr = str.Substring(4);
Console.WriteLine(substr); // "Script"
String Immutability:
In C#, string values are immutable, which means that they cannot be altered once created.
For example, the following code:
string myStr = "Bob";
myStr[0] = 'J'; // compile error!!!
cannot change the value of myStr to "Job", because the contents of myStr cannot be altered.
Note that this does not mean that myStr cannot be changed, just that the individual characters of a string cannot be changed. The only way to change myStr would be to assign it with a new string, like this:
string myStr = "Bob";
myStr = "Job";
//or
myStr = 'J' + myStr.Substring(1);
Immutability affects performance when concatenating many strings, it is recommended to use StringBuilder for performance
Convert a string to an array of characters:
When we have to change characters often in a string, we want a way of doing this quickly. Because strings are immutable, we change a character by creating a whole new string, which is O(n).
But arrays are mutable and changing an element in an array is O(1). So what we can do is first convert our string to an array of characters, operate on that array and than convert the array back to a string.
We can convert a string to an array using the ToCharArray() method like this:
string str = "Andy";
char[] myArr = str.ToCharArray();
// myArr is ['A', 'n', 'd', 'y']
myArr[0] = 'a';
myArr[2] = 'D';
// We convert an array of chars back to a string using the constructor:
str = new string(myArr);
Console.WriteLine(str); // "anDy"
Iterating through the characters:
We can iterate throught the elements of a string using indices and a for loop like this:
string myStr = "Andy";
for (int i = 0; i < myStr.Length; i++) {
Console.WriteLine(myStr[i]);
}
// This will print the characters 'A', 'n', 'd' and 'y' on different lines
This takes O(n) time, where n is the length of the string we iterate on.
Assignment
Follow the Coding Tutorial and let's play with some strings.
Hint
Look at the examples above if you get stuck.
Free plan: 30 tutor messages. PRO is unlimited.
