String.Substring Method (Int32, Int32)
Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.
Assembly: mscorlib (in mscorlib.dll)
The zero-based starting character position of a substring in this instance.
The number of characters in the substring.
startIndex plus length indicates a position not within this instance.
startIndex or length is less than zero.
You call the Substring(Int32) method.method to extract a substring from a string that begins at a specified character position and ends before 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 continues to the end of the string, call the
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.
The length parameter represents the total number of characters to extract from the current string instance. This includes the starting character found at index startIndex. In other words, themethod attempts to extract characters from index startIndex to index startIndex + length - 1.
If the substring extends from startIndex to a specified character sequence, you can call a method such as IndexOf or LastIndexOf to get the index of the ending character or character sequence. You can then convert that value to an index position in the string as follows:
If you've searched for a single character that is to mark the end of the substring, the length parameter equals endIndex - startIndex + 1, where endIndex is the return value of the IndexOf or IndexOf method. The following example extracts a continuous block of "b" characters from a string.
If you've searched for multiple characters that are to mark the end of the substring, the length parameter equals endIndex + endMatchLength - startIndex, where endIndex is the return value of the IndexOf or IndexOf method., and endMatchLength is the length of the character sequence that marks the end of the substring. The following example extracts a block of text that contains an XML <definition> element.
If the character or character sequence is not included in the end of the substring, the length parameter equals endIndex - startIndex, where endIndex is the return value of the IndexOf or IndexOf method.
If startIndex is equal to zero and equals the length of the current string, the method returns the original string unchanged.
The following example illustrates a simple call to themethod that extracts two characters from a string starting at the sixth character position (that is, at index five).
The following example uses themethod 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 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.
Available since 8
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Available since 8.1