.Substring()
The .Substring()
method is a string method that returns a substring of a string starting at the specified index. It will return all characters from that index to the end unless a maximum length is specified. If the starting index equals the string length, it returns an empty string (""
). If the index is greater than the string length, it throws an ArgumentOutOfRangeException
.
Syntax
.Substring(int startIndex)
Or, alternatively:
.Substring(int startIndex, int length)
startIndex
: The index from where the substring starts.Length
(Optional): The number of characters to include in the substring..
Example
In this example, .Substring()
is used to return the substring of “Codecademy” starting at index 4
and includes all characters from that position to the end of the string:
using System;public class Program{public static void Main(){string str = "Codecademy";Console.WriteLine(str.Substring(4));}}
The above example results in the following output:
cademy
Example 2
In this example, .Substring()
is used with the optional length
parameter to return a substring of 6 characters starting from index 2
of the string "Codecademy"
.
using System;public class Program{public static void Main(){string str = "Codecademy";Console.WriteLine(str.Substring(2, 6));}}
The above code generates the following output:
decade## Codebyte ExampleThe below code demonstrates how to use the Substring method:```codebyte/csharpusing System;public class Example{public static void Main(string[] args){string Name1 = "Brad";string Name2 = "Angelina";Console.WriteLine(Name1.Substring(1));Console.WriteLine(Name2.Substring(1));}}
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C# on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C#
Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.Beginner Friendly23 hours