String in C# full Explanation

In C#, the `string` data type represents a sequence of characters. It is a reference type and is used to store and manipulate text data. Here's a comprehensive explanation of strings in C#:

 

1. Declaration and Initialization:

v  You can declare and initialize a string variable using the `string` keyword.

v  Example:

    

     string name = "John";

    

 

2. Immutable Nature:

v  Strings in C# are immutable, which means that once a string is created, its value cannot be changed.

v  Any operation that modifies a string actually creates a new string.

v  Example:

    

     string message = "Hello";

     message += ", World!";  // Creates a new string, does not modify the original

    

 

3. Concatenation:

v  You can concatenate strings using the `+` operator or the `string.Concat` method.

v  Example:

    

     string firstName = "John";

     string lastName = "Doe";

     string fullName = firstName + " " + lastName;  // Using the + operator

     string fullNameConcat = string.Concat(firstName, " ", lastName);  // Using string.Concat method

    

 

4. String Interpolation:

v  String interpolation allows you to embed expressions within a string using the `$` prefix.

v  You can directly include variables or expressions within curly braces `{}`.

v  Example:

    

     int age = 30;

     string message = $"My age is {age}.";  // String interpolation

    

 

5. Accessing Characters:

v  You can access individual characters in a string using indexing, starting from 0.

v  The `[]` operator is used to access a specific character at a given index.

v  Example:

    

     string name = "John";

     char firstCharacter = name[0];  // Accessing the first character 'J'

    

 

6. String Length:

v  You can determine the length of a string using the `Length` property.

v  The `Length` property returns the number of characters in the string.

v  Example:

    

     string message = "Hello, World!";

     int length = message.Length;  // Length is 13

    

 

7. Common String Methods:

v  The `string` class provides various methods to manipulate and perform operations on strings.

v  Examples of common string methods include `ToLower`, `ToUpper`, `Substring`, `Replace`, `Split`, `Trim`, etc.

v  Example:

    

     string text = "   Hello, World!   ";

     string lowerCase = text.ToLower();  // "   hello, world!   "

     string upperCase = text.ToUpper();  // "   HELLO, WORLD!   "

     string trimmed = text.Trim();       // "Hello, World!"

    

 

8. String Comparison:

v  String comparison can be done using methods such as `Equals`, `Compare`, `CompareTo`, etc.

v  These methods allow you to determine the equality, order, or relative position of two strings.

v  Example:

    

     string str1 = "hello";

     string str2 = "world";

     bool areEqual = string.Equals(str1, str2);  // false

     int compareResult = string.Compare(str1, str2);  // -1 (str1 is less than str2)

    

 

Strings are extensively used for working with textual data in C# applications. Understanding the features and capabilities of strings allows you to manipulate and process text effectively in your programs.