Strings
In C#, a string is reference type data type that is used to represent a sequence of characters. It is declared with "string" keyword which is a wrapper around System.String class. We can also say that string is an array of characters represented in a concise way. String manipulation is a very important feature in any programming language because it is used to operate on textual data. In general, we can declare a string as follows.
Here name is a variable of string data type. To initialize a string, we simply assign a string literal to it.
We can simply use it just like any other variable. The following line will print "Alice".
We can also create string type variable from character array. The string constructor can take a character array type of argument to initialize a string.
String Concatenation
To concatenate two strings we use "+" operator. We did use "+" operator in the example in previous chapters. The below example will print "Hello World" to the console.
Strings are immutable
Consider the below example.
In the above example, the first statement will assign value "Alice" to variable "name". This value will be stored in the heap in memory. When the next statement gets executed, a new memory location will be allocated in the heap. The previous value in the heap will remain as it is unless the garbage collector cleans it up.
This is important to know because generally in programming we do a lot of string manipulation specially concatenation. So we must avoid such kind of extensive string manipulations because it will unintentionally consume memory.
Escape Characters
In C#, string literals allows us to provide certain escape characters which have some special meaning and provides certain behaviour. Some of those frequently used escape characters are "\n", "\t", "\"", "\\" etc. Let's see the below examples.
Note : There is an additional character "@" called "verbatim" which is used to write a string literal with backslash and newline without using the corresponding escape characters. Let's see an example.
String Interpolation
There are couple of ways in which we can manipulate string. Let's see those.
String.Format()
The "String.Format()" method takes a string literal as its first argument. In the rest of the arguments, we can pass values and variables which replace specific positions in the string defined by a place-holder - { }. Let see the below example.
In the above example, {0} will be replaced by values of "name" and {1} will be replaced by the string literal "good morning". The output would be "Hello Alice, good morning!"
The "$" prefix
This is very powerful and clean way of doing string interpolation which was introduced in C# 6.0. We can write any valid C# expression inside a string itself. In this, we prefix the string with "$" symbol. Let's see with an example.
In the above example, the variables name and greet replace the placeholders - {name} and {greet}. This will output the same string as in previous example - "Hello Alice, good morning!". Let's see another example.
Since the placeholders can take any valid C# expression, the above statement will output - "Square of 12 = 144" to the console. You can try with different valid C# expressions inside the placeholder.
Other string methods
Here are some of the commonly used string methods in C#.
Substring : It is used to get sub-string from a given string.
ToUpper : It is used to convert the string into upper case letters.
ToLower : It is used to convert the string into lower case letters.
Trim : It is used to remove the spaces before first character and after last character of a string.
Split : It is used to split a string into a string array based on a specific delimiter present in the string. The Split method takes a parameter called delimiter which is used to specify the splitting point within the string.
Contains : It is used to check whether a string contains a given substring or not.
Replace : It is used to replace a part of string with another string.
IndexOf : It returns the index of the first occurrence of a specified character or string.
LastIndexOf : It returns the index of the last occurrence of a specified character or string.
Here is the complete code below showing all different methods of string manipulations as described above.