C# String DataType

HTML
CSS
C#
SQL

Strings in C#

Strings are sequences of characters used to represent text or symbols. In C#, strings are represented by the string data type, which is a reference type. This means that strings are objects belonging to the System.String class and can be created and manipulated using various methods.

Creating Strings

There are different ways to create strings in C#. One way is by using string literals, which are enclosed in double quotes. For example:

string s1 = “Hello, World!”;

Another way is by using the string constructor, which takes an array of characters as an argument. Here’s an example:

char[] chars = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘,’, ‘ ‘, ‘W’, ‘o’, ‘r’, ‘l’, ‘d’, ‘!’ };

string s2 = new string(chars);

Concatenating Strings

To concatenate strings together, you can use the + operator or the string.Concat method. Here’s an example:

string s1 = “Hello”;

string s2 = “World”;

string s3 = s1 + “, ” + s2 + “!”;

string s4 = string.Concat(s1, “, “, s2, “!”);

Comparing Strings

When it comes to comparing strings in C#, you can use the == operator or the string.Equals method. Here’s an example:

string s1 = “Hello”;

string s2 = “World”;

bool b1 = s1 == s2; // false

bool b2 = string.Equals(s1, s2); // false

 

 

Modifying Strings

It’s important to note that strings in C# are immutable, meaning their values cannot be changed once they are created. However, you can create a new string that contains the modified value of the original string. For example:

string s1 = “Hello”;

string s2 = s1 + “, World!”; // creates a new string

String Length

To determine the length of a string in C#, you can use the Length property. For example:

string s1 = “Hello, World!”;

int length = s1.Length; // 13

Substrings

If you need to extract a substring from a larger string, you can use the Substring method. This method takes two arguments: the starting index and the length of the substring. Here’s an example:

string s1 = “Hello, World!”;

string s2 = s1.Substring(7, 5); // “World”

Searching Strings

Searching for specific substrings within a string is possible using the IndexOf method. This method returns the index of the first occurrence of a specified substring. For example:

string s1 = “Hello, World!”;

int index = s1.IndexOf(“World”); // 7

Modifying Case

If you need to modify the case of a string in C#, there are methods available such as ToUpper and ToLower. These methods convert strings to uppercase and lowercase respectively. Here’s an example:

string s1 = “Hello, World!”;

string s2 = s1.ToUpper(); // “HELLO, WORLD!”

string s3 = s1.ToLower(); // “hello, world!”

Removing Whitespace

To remove leading or trailing whitespace from a string in C#, you can use the Trim method. Here’s an example:

string s1 = ”   Hello, World!   “;

string s2 = s1.Trim(); // “Hello, World!”

Formatting Strings

When it comes to formatting strings in C#, you have options like the string.Format method or string interpolation. For example:

string name = “Alice”;

int age = 30;

string s1 = string.Format(“My name is {0} and I am {1} years old.”, name, age);

string s2 = $”My name is {name} and I am {age} years old.”; //string interpolation

Course Video