C# Strings


C# Strings

In this tutorial, we will learn about strings in C#. We will cover the basics of string manipulation, including creating, accessing, modifying, and performing operations on strings.


What is a String

A string in C# is an object of the System.String class. Strings are immutable, meaning their value cannot be changed once created. Strings are used for storing and handling text data.


Creating Strings

Strings can be created in C# using the string keyword:

string str = "Hello, world!";

Strings can also be created using the String class:

String str = "Hello, world!";


Initializing Strings

  1. Create a string variable and initialize it with a value.
  2. Print the string variable using Console.WriteLine.

C# Program

using System;
class Program {
    static void Main() {
        string str = "Hello, world!";
        Console.WriteLine(str);
    }
}

Output

Hello, world!


Accessing Characters in a String

  1. Create a string variable and initialize it with a value.
  2. Access and print individual characters using array indexing.

C# Program

using System;
class Program {
    static void Main() {
        string str = "Hello";
        Console.WriteLine(str[0]); // Accessing using array indexing
        Console.WriteLine(str[1]);
    }
}

Output

H
e


Modifying Strings

  1. Create a string variable and initialize it with a value.
  2. Strings in C# are immutable, so create a new string with the modified value.
  3. Print the modified string.

C# Program

using System;
class Program {
    static void Main() {
        string str = "Hello";
        str = 'J' + str.Substring(1); // Modifying individual character
        str += " World!"; // Appending new characters
        Console.WriteLine(str);
    }
}

Output

Jello World!


String Concatenation

  1. Create two string variables and initialize them with values.
  2. Concatenate the strings using the + operator or the String.Concat method.
  3. Print the concatenated string.

C# Program

using System;
class Program {
    static void Main() {
        string str1 = "Hello";
        string str2 = " World!";
        string str3 = str1 + str2; // Concatenating strings
        Console.WriteLine(str3);
    }
}

Output

Hello World!


Finding Substrings

  1. Create a string variable and initialize it with a value.
  2. Use the IndexOf method to find a substring.
  3. Print the position of the found substring.

C# Program

using System;
class Program {
    static void Main() {
        string str = "Hello, world!";
        int pos = str.IndexOf("world"); // Finding substring
        if (pos != -1) {
            Console.WriteLine("Found 'world' at position: " + pos);
        } else {
            Console.WriteLine("Substring not found");
        }
    }
}

Output

Found 'world' at position: 7


String Length

  1. Create a string variable and initialize it with a value.
  2. Use the Length property to get the length of the string.
  3. Print the length of the string.

C# Program

using System;
class Program {
    static void Main() {
        string str = "Hello, world!";
        Console.WriteLine("Length of the string: " + str.Length); // Getting string length
    }
}

Output

Length of the string: 13