Export (0) Print
Expand All

String.ToCharArray Method (Int32, Int32)

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

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public char[] ToCharArray (
	int startIndex,
	int length
)
public char[] ToCharArray (
	int startIndex, 
	int length
)
public function ToCharArray (
	startIndex : int, 
	length : int
) : char[]
Not applicable.

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.

Exception typeCondition

ArgumentOutOfRangeException

startIndex or length is less than zero.

-or-

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

The startIndex parameter is zero-based. That is, the index of the first character in the string instance is zero.

If length is zero, the returned array is empty and has a zero length. If this instance is a null reference (Nothing in Visual Basic) or an empty string (""), the returned array is empty and has a zero length.

The following code 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 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
*/

// Sample for String.ToCharArray(Int32, Int32)
import System.*;

class Sample
{
    public static void main(String[] args)
    {
        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);
        for (int iCtr = 0; iCtr < arr.length; iCtr++) {
            char c = arr[iCtr];
            Console.WriteLine(c);
        }
    } //main
} //Sample
/*
This example produces the following results:
The letters in '012wxyz789' are: 'wxyz'
Each letter in '012wxyz789' is:
w
x
y
z
*/

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft