Substring Method (Int32, Int32)
Collapse the table of content
Expand the table of content

String.Substring Method (Int32, Int32)


Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public string Substring(
	int startIndex,
	int length


Type: System.Int32

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

Type: System.Int32

The number of characters in the substring.

Return Value

Type: System.String

A string that is equivalent to the substring of length length that begins at startIndex in this instance, or Empty if startIndex is equal to the length of this instance and length is zero.

Exception Condition

startIndex plus length indicates a position not within this instance.


startIndex or length is less than zero.

startIndex is zero-based.


This method does not modify the value of the current instance. Instead, it returns a new string with length characters starting from the startIndex position in the current string.

If startIndex is equal to zero and equals the length of the current string, the method rlengtheturns the original string unchanged.

The following example uses the Substring method in the following three cases to isolate substrings within a string:. In two cases the substrings are used in comparisons, and in the third case an exception is thrown because invalid parameters are specified.

  • It extracts the single character and the third position in the string (at index 2) and compares it with a "c". This comparison returns true.

  • It extracts zero characters starting at the fourth position in the string (at index 3) and passes it to the IsNullOrEmpty method. This returns true because the call to the Substring method returns String.Empty.

  • It attempts to extract one character starting at the fourth position in the string. Because there is no character at that position, the method call throws an ArgumentOutOfRangeException exception.

using System;

public class Sample
   public static void Main() {
      String myString = "abc";
      bool test1 = myString.Substring(2, 1).Equals("c"); // This is true.
      bool test2 = String.IsNullOrEmpty(myString.Substring(3, 0)); // This is true.
      try {
         string str3 = myString.Substring(3, 1); // This throws ArgumentOutOfRangeException.
      catch (ArgumentOutOfRangeException e) {
// The example displays the following output:
//       True
//       True
//       Index and length must refer to a location within the string.
//       Parameter name: length

The following example uses the Substring method to separate key/value pairs that are delimited by an equals ("=") character.

using System;

public class Example
   public static void Main()
      String[] pairs = { "Color1=red", "Color2=green", "Color3=blue",
                         "Title=Code Repository" };
      foreach (var pair in pairs) {
         int position = pair.IndexOf("=");
         if (position < 0)
         Console.WriteLine("Key: {0}, Value: '{1}'", 
                           pair.Substring(0, position),
                           pair.Substring(position + 1));
// The example displays the following output:
//     Key: Color1, Value: 'red'
//     Key: Color2, Value: 'green'
//     Key: Color3, Value: 'blue'
//     Key: Title, Value: 'Code Repository'

The IndexOf method is used to get the position of the equals character in the string.. The call to the Substring(Int32, Int32) method extracts the key name., which starts from the first character in the string and extends for the number of characters returned by the call to the IndexOf method. The call to the Substring(Int32) method then extracts the value assigned to the key. It starts at one character position beyond the equals character and extends to the end of the string.

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
© 2015 Microsoft