This documentation is archived and is not being maintained.

String.Substring Method (Int32, Int32)

.NET Framework 1.1

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

[Visual Basic]
Overloads Public Function Substring( _
   ByVal startIndex As Integer, _
   ByVal length As Integer _
) As String
[C#]
public string Substring(
 int startIndex,
 int length
);
[C++]
public: String* Substring(
 int startIndex,
 int length
);
[JScript]
public function Substring(
   startIndex : int,
 length : int
) : String;

Parameters

startIndex
The index of the start of the substring.
length
The number of characters in the substring.

Return Value

A String 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.

Exceptions

Exception Type Condition
ArgumentOutOfRangeException startIndex plus length indicates a position not within this instance.

-or-

startIndex or length is less than zero.

Remarks

startIndex is zero-based.

Example

[Visual Basic] 
Dim myString As String = "abc"
Dim test1 As Boolean = String.Compare(myString.Substring(2, 1), "c") = 0 ' This is true.
myString.Substring(3, 1) ' This throws ArgumentOutOfRangeException.
Dim test2 As Boolean = String.Compare(myString.Substring(3, 0), String.Empty) = 0 ' This is true.

[C#] 
String myString = "abc";
bool test1 = String.Compare(myString.Substring(2, 1), "c") == 0; // This is true.
myString.Substring(3, 1); // This throws ArgumentOutOfRangeException.
bool test2 = String.Compare(myString.Substring(3, 0), String.Empty) == 0; // This is true.

[C++] 

String *myString = L"abc";
bool test1 = String::Compare(myString->Substring(2,1), L"c") == 0;  // This is true.
myString->Substring(3,1);  // This throws ArgumentOutOfRangeException.
bool test2 = String::Compare(myString->Substring(3,0), String::Empty) == 0;  // This is true.

[JScript] 
var myString : String = "abc";
var test1 : boolean = String.Compare(myString.Substring(2, 1), "c") == 0; // This is true.
myString.Substring(3, 1);  // This throws ArgumentOutOfRangeException.
var test2 : boolean = String.Compare(myString.Substring(3, 0), String.Empty) == 0; // This is true.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

String Class | String Members | System Namespace | String.Substring Overload List | Int32 | Concat | Insert | Join | Remove | Replace | Split | Trim

Show: