Edit

Share via


String.ToCharArray Method

Definition

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

Overloads

ToCharArray(Int32, Int32)

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

ToCharArray()

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

ToCharArray(Int32, Int32)

Source:
String.cs
Source:
String.cs
Source:
String.cs

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

public char[] ToCharArray(int startIndex, int length);

Parameters

startIndex
Int32

The starting position of a substring in this instance.

length
Int32

The length of the substring in this instance.

Returns

Char[]

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

Exceptions

startIndex or length is less than zero.

-or-

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

Examples

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 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
*/

Remarks

This method copies the characters in a portion of a string to a character array. To create a string from a range of characters in a character array, call the String(Char[], Int32, Int32) constructor.

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 null or an empty string (""), the returned array is empty and has a zero length.

To create a byte array that contains the encoded characters in a portion of a string, instantiate the appropriate Encoding object and call its GetBytes(String, Int32, Int32, Byte[], Int32) method. Some of the standard encodings available in .NET include:

Encoding Object
ASCII ASCIIEncoding
UTF-7 UTF7Encoding
UTF-8 UTF8Encoding
UTF-16 UnicodeEncoding
UTF-32 UTF32Encoding

For more information, see Character Encoding in .NET.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToCharArray()

Source:
String.cs
Source:
String.cs
Source:
String.cs

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

public char[] ToCharArray();

Returns

Char[]

A Unicode character array whose elements are the individual characters of this instance. If this instance is an empty string, the returned array is empty and has a zero length.

Examples

The following example calls the ToCharArray method to extract the characters in a string to a character array. It then displays the original string and the elements in the array.

using System;

public class Example
{
   public static void Main()
   {
      string s = "AaBbCcDd";
      char[] chars = s.ToCharArray();
      Console.WriteLine("Original string: {0}", s);
      Console.WriteLine("Character array:");
      for (int ctr = 0; ctr < chars.Length; ctr++)
      {
         Console.WriteLine("   {0}: {1}", ctr, chars[ctr]);
      }
   }
}

// The example displays the following output:
//     Original string: AaBbCcDd
//     Character array:
//        0: A
//        1: a
//        2: B
//        3: b
//        4: C
//        5: c
//        6: D
//        7: d

Remarks

This method copies each character (that is, each Char object) in a string to a character array. The first character copied is at index zero of the returned character array; the last character copied is at index Array.Length - 1.

To create a string from the characters in a character array, call the String(Char[]) constructor.

To create a byte array that contains the encoded characters in a string, instantiate the appropriate Encoding object and call its Encoding.GetBytes(String) method. Some of the standard encodings available in .NET include the following:

Encoding Object
ASCII ASCIIEncoding
UTF-7 UTF7Encoding
UTF-8 UTF8Encoding
UTF-16 UnicodeEncoding
UTF-32 UTF32Encoding

For more information, see Character Encoding in .NET.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0