This documentation is archived and is not being maintained.
String::ToCharArray Method (Int32, Int32)
Visual Studio 2010
Copies the characters in a specified substring in this instance to a Unicode character array.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- startIndex
- Type: System::Int32
The starting position of a substring in this instance.
- length
- Type: System::Int32
The length of the substring in this instance.
Return Value
Type: array<System::Char>A Unicode character array whose elements are the length number of characters in this instance starting from character position startIndex.
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | startIndex or length is less than zero. -or- startIndex plus length is greater than the length of this instance. |
The following example converts a substring within a string to an array of characters, then enumerates and displays the elements of the array.
// Sample for String::ToCharArray(Int32, Int32) using namespace System; using namespace System::Collections; int main() { String^ str = "012wxyz789"; array<Char>^arr; arr = str->ToCharArray( 3, 4 ); Console::Write( "The letters in '{0}' are: '", str ); Console::Write( arr ); Console::WriteLine( "'" ); Console::WriteLine( "Each letter in '{0}' is:", str ); IEnumerator^ myEnum = arr->GetEnumerator(); while ( myEnum->MoveNext() ) { Char c = safe_cast<Char>(myEnum->Current); Console::WriteLine( c ); } } /* This example produces the following results: The letters in '012wxyz789' are: 'wxyz' Each letter in '012wxyz789' is: w x y z */
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show: