添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft Edge More info about Internet Explorer and Microsoft Edge
public:
 cli::array <char> ^ ToCharArray(int startIndex, int length);
public char[] ToCharArray (int startIndex, int length);
member this.ToCharArray : int * int -> char[]
Public Function ToCharArray (startIndex As Integer, length As Integer) As Char()

Parameters

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 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:
// 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:
// Sample for String.ToCharArray(Int32, Int32)
let str = "012wxyz789"
let arr = str.ToCharArray(3, 4)
printf $"The letters in '{str}' are: '"
printf $"{arr}"
printfn "'"
printfn $"Each letter in '{str}' is:"
for c in arr do
    printfn $"{c}"
This example produces the following results:
The letters in '012wxyz789' are: 'wxyz'
Each letter in '012wxyz789' is:
' Sample for String.ToCharArray(Int32, Int32)
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
End Class
'This example produces the following results:
'The letters in '012wxyz789' are: 'wxyz'
'Each letter in '012wxyz789' is:
    	

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 cli::array <char> ^ ToCharArray();
public char[] ToCharArray ();
member this.ToCharArray : unit -> char[]
Public Function ToCharArray () As Char()

Returns

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
let s = "AaBbCcDd"
let chars = s.ToCharArray()
printfn $"Original string: {s}"
printfn "Character array:"
for i = 0 to chars.Length - 1 do
    printfn $"   {i}: {chars[i]}"
// 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
Module Example
   Public Sub Main()
      Dim s As String = "AaBbCcDd"
      Dim chars() = s.ToCharArray()
      Console.WriteLine("Original string: {0}", s)
      Console.WriteLine("Character array:")
      For ctr = 0 to chars.Length - 1
         Console.WriteLine("   {0}: {1}", ctr, chars(ctr))
   End Sub
End Module
' 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