C# String SubString()

The Substring() method in C# is used to extract a portion of a string. It returns a new string that represents a specified portion of the original string.

The Substring() method has a couple of overloaded versions:

  1. Substring(int startIndex): This version returns a new string that starts from the specified startIndex and extends to the end of the original string. The characters at the startIndex position and all subsequent characters are included in the substring. Here’s an example:
string originalString = "Hello, World!";
string substring = originalString.Substring(7);
Console.WriteLine(substring); // Output: "World!"

2. Substring(int startIndex, int length): This version returns a new string that starts from the specified startIndex and includes the specified length of characters. The length parameter determines the number of characters to include in the substring. Here’s an example:

string originalString = "Hello, World!";
string substring = originalString.Substring(0, 5);
Console.WriteLine(substring); // Output: "Hello"

It’s important to note that the Substring() method doesn’t modify the original string; it creates a new string that represents the desired substring.

Signature:

The signature of the Substring() method in C# is as follows:

  1. Substring(int startIndex): string This version takes an int parameter startIndex and returns a string. It returns a new string that starts from the specified startIndex and extends to the end of the original string.
  2. Substring(int startIndex, int length): string This version takes two int parameters, startIndex and length, and returns a string. It returns a new string that starts from the specified startIndex and includes the specified length of characters.

Both versions of the Substring() method are available for use with strings in C#.

Parameter:

The Substring() method in C# accepts the following parameters:

  1. startIndex (required): This parameter specifies the starting index of the substring. It is of type int and represents the position within the original string from which the substring should begin. The index is zero-based, meaning the first character of the string has an index of 0.
  2. length (optional): This parameter specifies the number of characters to include in the substring. It is of type int and determines the length of the substring. If this parameter is omitted, the substring will extend from the startIndex to the end of the original string.

The startIndex parameter is always required, while the length parameter is optional and can be omitted if you want the substring to extend to the end of the string.

Return:

The Substring() method in C# returns a new string that represents the desired substring. The type of the return value is string. The returned string contains the characters extracted from the original string based on the specified starting index and length, or from the starting index to the end of the string if the length is not specified.

Here are a few examples:

string originalString = "Hello, World!";
string substring1 = originalString.Substring(7);
Console.WriteLine(substring1); // Output: "World!"

string substring2 = originalString.Substring(0, 5);
Console.WriteLine(substring2); // Output: "Hello"

In the first example, the Substring(7) call returns a substring starting from the index 7 (“W”) and extends to the end of the original string, resulting in the output “World!”.

In the second example, the Substring(0, 5) call returns a substring starting from the index 0 (“H”) and includes the next 5 characters, resulting in the output “Hello”.

C# String SubString() Method Example:

Certainly! Here’s an example demonstrating the usage of the Substring() method in C#:

using System;

class Program
{
    static void Main()
    {
        string originalString = "Hello, World!";
        
        // Extract substring from index 7 to the end
        string substring1 = originalString.Substring(7);
        Console.WriteLine(substring1); // Output: "World!"
        
        // Extract substring from index 0 with length 5
        string substring2 = originalString.Substring(0, 5);
        Console.WriteLine(substring2); // Output: "Hello"
        
        // Extract substring from index 7 with length 5
        string substring3 = originalString.Substring(7, 5);
        Console.WriteLine(substring3); // Output: "World"
    }
}

In this example, we have a string originalString with the value “Hello, World!”. We then use the Substring() method to extract different substrings from it.

  • substring1 extracts the substring starting from index 7 to the end of the string, resulting in the output “World!”.
  • substring2 extracts the substring starting from index 0 with a length of 5 characters, resulting in the output “Hello”.
  • substring3 extracts the substring starting from index 7 with a length of 5 characters, resulting in the output “World”.

These examples showcase different scenarios of using the Substring() method to extract substrings based on different starting indices and lengths.