C# String ToCharArray(Int32, Int32)

Description

String ToCharArray(Int32, Int32) copies the characters in a specified substring in this instance to a Unicode character array.

Syntax

String.ToCharArray(Int32, Int32) has the following syntax.


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

Parameters

String.ToCharArray(Int32, Int32) has the following parameters.

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

Returns

String.ToCharArray(Int32, Int32) method returns

Example

The following example converts a substring within a string to an array of characters, then enumerates and displays the elements of the array.


using System;//www .java2  s  .co m

class Sample {
    public static void Main() {
        string str = "this is a test";
        char[] arr;
    
        arr = str.ToCharArray(3, 4);
        foreach (char c in arr)
           Console.WriteLine(c);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version