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
)

Parameters

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
ArgumentOutOfRangeException

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

You call the Substring method to extract a substring from a string that begins at a specified character position and ends at the end of the string. The starting character position is a zero-based; in other words, the first character in the string is at index 0, not index 1. To extract a substring that begins at a specified character position and ends before to the end of the string, call the Substring method.

System_CAPS_noteNote

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.

To extract a substring that begins with a particular character or character sequence, call a method such as IndexOf or IndexOf to get the value of startIndex. The second example illustrates this; it extracts a key value that begins one character position after the "=" character.

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

The following example demonstrates obtaining a substring from a string.

using namespace System;
using namespace System::Collections;

int main()
{
   array<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:");
   for each (String^ s in info) 
      Console::WriteLine(s);

   Console::WriteLine("\nWe want to retrieve only the key information. That is:");
   for each (String^ s in info) { 
      found = s->IndexOf(": ");
      Console::WriteLine("   {0}", s->Substring(found + 2));
   }
}
// The example displays the following output:
//       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.

No code example is currently available or this language may not be supported.

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 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show:
© 2016 Microsoft