Skip to content
Docs
தமிழ்
Esc
↑↓navigate↵open⌘Jpreview
On this page

Strings in C#

Overview

  • Reference Data Type: Strings are a reference data type in C#.
  • Storage: They are used to store sequences of character objects, representing text.
  • Class: A string in C# is an object of the System.String class.
  • Operations: The String class provides a wide range of methods to perform various operations on strings.

Declaration & Initialization

  1. Using string Keyword:

    • Declaration:

      string name; // String declaration
    • Declaration and Initialization:

      string name = "Kamal"; // Declaration and initialization at once
  2. Creating an Object from System.String Class:

    • Declaration:

      String homeTown; // Declaration
    • Declaration and Initialization:

      String homeTown = "Colombo"; // Declaration and initialization at once

Common String Methods

Here are some commonly used methods from the System.String class:

  1. Clone()

    • Description: Returns a reference to this instance of the string.
    • Example:
      string original = "Hello";
      string copy = (string)original.Clone();
  2. Compare(String, String)

    • Description: Compares two string objects.
    • Example:
      int result = String.Compare("Apple", "Banana");
      // result will be less than 0 because "Apple" is less than "Banana"
  3. Concat(String, String)

    • Description: Concatenates two specified strings.
    • Example:
      string fullName = String.Concat("John", "Doe");
      // fullName will be "JohnDoe"
  4. ToString()

    • Description: Returns an instance of the string.
    • Example:
      int number = 123;
      string numberString = number.ToString();
  5. Trim()

    • Description: Removes leading and trailing whitespace characters.
    • Example:
      string text = "  Hello  ";
      string trimmedText = text.Trim(); // "Hello"
  6. ToUpper()

    • Description: Converts a string into uppercase.
    • Example:
      string text = "hello";
      string upperText = text.ToUpper(); // "HELLO"
  7. IndexOf(char)

    • Description: Gets the index of the specified character in the string.
    • Example:
      string text = "Hello";
      int index = text.IndexOf('e'); // index will be 1
  8. Substring(Int32)

    • Description: Retrieves a substring from this instance starting at a specified index.
    • Example:
      string text = "HelloWorld";
      string subText = text.Substring(5); // subText will be "World"
  9. Join(String, String[])

    • Description: Concatenates all the elements of a string array, using the specified separator.
    • Example:
      string[] words = { "Hello", "World" };
      string sentence = String.Join(" ", words); // sentence will be "Hello World"

Last updated on 7 அக்டோபர், 2026

Was this page helpful?