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

String.Substring Method (Int32)


Retrieves a substring from this instance. The substring starts at a specified character position and continues to the end of the string.

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

public string Substring(
	int startIndex


Type: System.Int32

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

Return Value

Type: System.String

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

Exception Condition

startIndex is less than zero or greater than the length of this instance.

The index is zero-based.


This method does not modify the value of the current instance. Instead, it returns a new string that begins at the startIndex position in the current string.

If startIndex is equal to zero, the method returns the original string unchanged.

The following example demonstrates obtaining a substring from a string.

using System;

public class SubStringTest {
    public static void Main() {

        string [] info = {"Name: Felica Walker", "Title: Mz.", "Age: 47", "Location: Paris", "Gender: F"};
        int found = 0;

        Console.WriteLine("The initial values in the array are:");
        foreach (string s in info)

        Console.WriteLine("{0}We want to retrieve only the key information. That is:", Environment.NewLine);        

        foreach (string s in info) {
            found = s.IndexOf(":");
            Console.WriteLine(s.Substring(found + 1));
// The example displays the following output to the console:
//       The initial values in the array are:
//       Name: Felica Walker
//       Title: Mz.
//       Age: 47
//       Location: Paris
//       Gender: F
//       We want to retrieve only the key information. That is:
//       Felica Walker
//       Mz.
//       47
//       Paris
//       F

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