In this blog we will see what are the different types of strings in c# and we will discuss in detail about the different ways of creating a string.
String manipulation is the most common part of many c# programs.Strings is a sequence of characters.The other and easiest way to represent a stream of characters in c# is by using character array.For example
char[] charArray = new char[4];
charArray[0] = 'h';
charArray[1] = 'e';
charArray[2] = 'l';
charArray[3] = 'l';
charArray[4] = 'o';
- Even though by using character array we can access any element by specifying the particular subscript it does not support the wide range of operations that we want to perform on the strings.c# provides solutions to handle these kind of situations.
- c# supports two types of strings namely immutable and mutable strings.In this blog we will see all the common operations that is performed on these two types of strings.
- c# also supports a concept known as regular expressions that can be used for complex string manipulations and pattern matching.
Creating Strings
- c# contains a predefined reference type known as string. We can use string datatype to declare string variables , when we declare a string using a string type, we are in fact declaring an object to be of type System.String, one of the built-in types provided in the .NET framework.
- A string type is same as same as System.String type.
We can create immutable strings in a number of ways.Some of them are listed below:
- Assigning string literals
- copying from one object to another
- concatenating two objects
- reading from the keyboard
- using ToString method
Assigning string literals:
- The most commonly used to create a string is to assign a sequence of characters known as string literal to string object.
For example:
String s; // declaring a string object
s = " hello "; // assigning string literal (hello) to the string object
- The above two statements can be combined and written as a single statement:
String s = " hello "; //declaring and assigning
Copying strings
We can can create new copies of existing strings and this can be done in two ways namely:
- using overloaded (=) operator
String s2 = s1;
- using the static copy method
String s2 = String.Copy(s1);
Both the above statements will copy the contents from the string s1 to s2.
Concatenating Strings:
We can create a new string by concatenating the already available strings.We can achieve this in two ways:
- using the overloaded operator
String s3 = s1 + s2; // s1 and s2 already exist
- using the static concat method
String s3 = String.Concat( s1 , s2 );
If s1 = ” Hello ” and s2 = ” World ” then after concatenation both these statements (i.e) HelloWorld will be stored in s3.
Reading from the keyboard:
We can actually get the values for the string from the user and store it in a string object as follows:
String s = Console.ReadLine();
On reaching this statement the computer waits for the user to enter a stream of characters from the keyboard.When the return key is pressed the values are stored in the string object here s.
The ToString method:
We can also create the string by calling the ToString method.
int number = 452;
String s = number.ToString();
here the number is converted to string using the ToString method and stored in the string object s.