C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# Substring: String Interception Function

In C#, the Substring method of the System.String class allows you to extract a portion of a string based on a specified index and length. In this tutorial, we'll cover the basics of using the Substring method effectively.

  • Substring

The Substring method has two overloaded versions:

public string Substring(int startIndex)
public string Substring(int startIndex, int length)
  • startIndex: The zero-based index position from which to start the extraction.
  • length (optional): The number of characters to extract. If not provided, the method extracts all characters from the startIndex to the end of the string.

The method returns a new string containing the specified portion of the original string.

Examples:

string original = "The quick brown fox jumps over the lazy dog.";

string substring1 = original.Substring(16); // "fox jumps over the lazy dog."
string substring2 = original.Substring(16, 3); // "fox"

Note that strings in C# are immutable, which means that the Substring method does not modify the original string. Instead, it creates a new string with the desired content.

  • Handling ArgumentOutOfRangeException

The Substring method throws an ArgumentOutOfRangeException if the startIndex is less than 0 or greater than the length of the string, or if the length is less than 0 or greater than the remaining length of the string from the startIndex.

To avoid this exception, you can validate the input before calling the Substring method:

string original = "The quick brown fox jumps over the lazy dog.";

int startIndex = 16;
int length = 3;

if (startIndex >= 0 && startIndex < original.Length && length >= 0 && startIndex + length <= original.Length)
{
    string substring = original.Substring(startIndex, length); // "fox"
}
else
{
    // Handle invalid input
}

In this tutorial, we covered how to extract a portion of a string using the Substring method in C#. Remember that strings are immutable, so the Substring method returns a new string with the specified content. Be sure to handle potential ArgumentOutOfRangeException cases by validating the input before calling the method.

  1. How to use Substring in C#

    The Substring method in C# is used to extract a portion of a string based on specified parameters.

    string originalString = "C# programming";
    string subString = originalString.Substring(2, 6); // Returns " programming"
    
  2. Extracting substrings from a string in C#

    Use Substring to extract a substring from a given string.

    string text = "Hello, World!";
    string extracted = text.Substring(7); // Returns "World!"
    
  3. C# Substring start and length parameters

    Specify both start index and length parameters to define the substring.

    string message = "C# is powerful";
    string snippet = message.Substring(3, 2); // Returns " is"
    
  4. Substring vs. Substr in C#

    In C#, the correct method is Substring, and there is no Substr method.

    string original = "C# programming";
    string result = original.Substring(2, 6); // Returns " program"
    
  5. Getting the first N characters with Substring in C#

    Extract the first N characters from a string using Substring.

    string input = "C# Programming";
    int length = 3;
    string result = input.Substring(0, length); // Returns "C# "
    
  6. Extracting the last N characters with Substring in C#

    Use Substring to extract the last N characters from a string.

    string text = "C# Rocks!";
    int lastCharacters = 5;
    string result = text.Substring(text.Length - lastCharacters); // Returns "Rocks"
    
  7. Substring and index-based extraction in C#

    Extract a substring based on index values.

    string sentence = "C# is amazing";
    int startIndex = 3;
    int endIndex = 8;
    string extracted = sentence.Substring(startIndex, endIndex - startIndex); // Returns " is a"
    
  8. Using Substring for string truncation in C#

    Truncate a string to a specified length using Substring.

    string longText = "This is a very long text.";
    int maxLength = 10;
    string truncated = longText.Substring(0, maxLength); // Returns "This is a"
    
  9. Substring for extracting parts of a file path in C#

    Extract specific parts of a file path using Substring.

    string filePath = @"C:\Documents\Example.txt";
    string fileName = filePath.Substring(filePath.LastIndexOf('\\') + 1); // Returns "Example.txt"
    
  10. Substring and string manipulation in C#

    Combine Substring with other string manipulation operations.

    string input = "C# Programming";
    string result = input.Substring(3, 7).ToUpper(); // Returns "PROGRA"
    
  11. Handling out-of-bounds scenarios with Substring in C#

    Ensure that the specified indices are within the bounds of the string.

    string text = "C# Programming";
    int startIndex = 10;
    if (startIndex < text.Length)
    {
        string result = text.Substring(startIndex); // Returns "gramming"
    }
    
  12. Substrings and character encoding in C#

    Substring operates on Unicode characters, and character encoding doesn't affect its behavior.

    string unicodeText = "C# �ַ���";
    string result = unicodeText.Substring(3, 3); // Returns "�ַ���"