Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
String.Substring Method (startIndex)
2.1.
Syntax
2.2.
Parameters
2.3.
Return Value
2.4.
Exceptions
2.5.
Example
2.6.
C#
3.
String.Substring Method (int startIndex, int length)
3.1.
Syntax
3.2.
Parameters
3.3.
Return Value
3.4.
Exceptions:
3.5.
Example
3.6.
C#
4.
Frequently Asked Questions
4.1.
What happens if startIndex is equal to the string's length?
4.2.
Can Substring be used with arrays or only strings?
4.3.
Is it possible to modify the string returned by Substring?
5.
Conclusion
Last Updated: Jul 25, 2024
Easy

C# Substring

Introduction

Just like every other language, C# also provides a powerful method called Substring that allows you to extract a portion of a string. This method is very useful when you need to work with specific parts of a string, such as extracting a name from a full name or getting a specific section of a long text. 

C# Substring

In this article, we will learn about the Substring method in C#, how to use it with different parameters with proper examples for better understanding.

String.Substring Method (startIndex)

Syntax

public string Substring(int startIndex);

Parameters

startIndex: The zero-based starting character position of a substring in this instance.

Return Value

The method returns a string that begins at startIndex and continues to the end of the string.

Exceptions

ArgumentOutOfRangeException: Thrown if startIndex is less than zero or greater than the length of the string.

Example

  • C#

C#

string example = "Hello, world!";

string substring = example.Substring(7);

Console.WriteLine(substring);

Output: 

"world!"


This example shows how the Substring method extracts everything from the eighth character to the end of the string, based on the zero-based index provided in startIndex.

String.Substring Method (int startIndex, int length)

Syntax

public string Substring(int startIndex, int length);

Parameters

  • startIndex: The zero-based starting character position of a substring in this instance
     
  • length: The number of characters in the substring.

Return Value

This method returns a string that starts at startIndex and has a specified number of characters length.

Exceptions:

ArgumentOutOfRangeException: Thrown if startIndex or startIndex + length is out of range.

Example

  • C#

C#

string example = "Hello, world!";

string substring = example.Substring(0, 5);

Console.WriteLine(substring);

Output: 

"Hello"


In this example, the Substring method extracts the first five characters from the string. It starts at index 0 and continues to index 4, which makes up the substring "Hello". This function is highly useful for tasks such as extracting specific parts of strings based on known indices and lengths.

Frequently Asked Questions

What happens if startIndex is equal to the string's length?

If startIndex equals the string's length, the Substring method returns an empty string. This is because the starting position does not precede any characters within the string.

Can Substring be used with arrays or only strings?

The Substring method is specifically designed for use with strings. To extract elements from an array, you would use different methods, such as array slicing or LINQ queries in C#.

Is it possible to modify the string returned by Substring?

Strings in C# are immutable, which means the string returned by Substring cannot be modified directly. However, you can manipulate the returned string by creating a new string based on operations performed on the substring.

Conclusion

In this article, we learned about the Substring method in C#, which allows us to extract a portion of a string. We discussed two overloads of the method: one that takes only the starting index and another that takes both the starting index and the length of the substring. We also looked at examples to understand how to use the Substring method effectively. 

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.

Live masterclass