Export (0) Print
Expand All

String.ToCharArray Method (Int32, Int32)

.NET Framework 1.1

Copies the characters in a specified substring in this instance to a Unicode character array.

[Visual Basic]
Overloads Public Function ToCharArray( _
   ByVal startIndex As Integer, _
   ByVal length As Integer _
) As Char()
[C#]
public char[] ToCharArray(
 int startIndex,
 int length
);
[C++]
public: __wchar_t ToCharArray(
 int startIndex,
 int length
)  __gc[];
[JScript]
public function ToCharArray(
   startIndex : int,
 length : int
) : Char[];

Parameters

startIndex
The starting position of a substring in this instance.
length
The length of the substring in this instance.

Return Value

A Unicode character array whose elements are the length number of characters in this instance starting from character position startIndex.

Exceptions

Exception Type Condition
ArgumentOutOfRangeException startIndex or length is less than zero.

-or-

startIndex plus length is greater than the length of this instance.

Remarks

If length is zero, the entire string is copied starting from the beginning of this instance, and ignoring the value of startIndex. If this instance is an empty string, the returned array is empty and has a zero length.

Example

[Visual Basic] 
' Sample for String.ToCharArray(Int32, Int32)
Imports System

Class Sample
   
   Public Shared Sub Main()
      Dim str As String = "012wxyz789"
      Dim arr() As Char
      
      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)
      Dim c As Char
      For Each c In arr
         Console.WriteLine(c)
      Next c
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'The letters in '012wxyz789' are: 'wxyz'
'Each letter in '012wxyz789' is:
'w
'x
'y
'z
'

[C#] 
// Sample for String.ToCharArray(Int32, Int32)
using System;

class Sample {
    public static void Main() {
    string str = "012wxyz789";
    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);
    foreach (char c in arr)
        Console.WriteLine(c);
    }
}
/*
This example produces the following results:
The letters in '012wxyz789' are: 'wxyz'
Each letter in '012wxyz789' is:
w
x
y
z
*/

[C++] 
// Sample for String::ToCharArray(Int32, Int32)
#using <mscorlib.dll>

using namespace System;
using namespace System::Collections;

int main()
{
   String* str = S"012wxyz789";
   Char arr[];

   arr = str->ToCharArray(3, 4);
   Console::Write(S"The letters in '{0}' are: '", str);
   Console::Write(arr);
   Console::WriteLine(S"'");
   Console::WriteLine(S"Each letter in '{0}' is:", str);
   IEnumerator* myEnum = arr->GetEnumerator();
   while (myEnum->MoveNext()) {
      Char c = *__try_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
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

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

See Also

String Class | String Members | System Namespace | String.ToCharArray Overload List | Array | Char | Int32

Show:
© 2014 Microsoft