String.ToCharArray 方法

定义

将此实例中的字符复制到 Unicode 字符数组。

重载

ToCharArray(Int32, Int32)

将此实例中的指定子字符串内的字符复制到 Unicode 字符数组。

ToCharArray()

将此实例中的字符复制到 Unicode 字符数组。

ToCharArray(Int32, Int32)

将此实例中的指定子字符串内的字符复制到 Unicode 字符数组。

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()

参数

startIndex
Int32

此实例内子字符串的起始位置。

length
Int32

此实例内子字符串的长度。

返回

Char[]

元素为此实例中从字符位置 length 开始的 startIndex 字符数的 Unicode 字符数组。

例外

startIndexlength 小于零。

startIndexlength 大于此实例的长度。

示例

以下示例将字符串中的子字符串转换为字符数组,然后枚举并显示数组的元素。

// 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:
w
x
y
z
*/
// 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)
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:
w
x
y
z
*)
' 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:
'w
'x
'y
'z
'

注解

此方法将字符串的一部分字符复制到字符数组中。 若要从字符数组中的字符范围创建字符串,请 String(Char[], Int32, Int32) 调用 构造函数。

参数 startIndex 从零开始。 也就是说,字符串实例中第一个字符的索引为零。

如果 length 为零,则返回的数组为空,长度为零。 如果此实例为 null 或空字符串 (“”) ,则返回的数组为空且长度为零。

若要创建包含字符串部分编码字符的字节数组,请实例化相应的 Encoding 对象并调用其 GetBytes(String, Int32, Int32, Byte[], Int32) 方法。 .NET 中提供的一些标准编码包括:

编码 对象
ASCII ASCIIEncoding
UTF-7 UTF7Encoding
UTF-8 UTF8Encoding
UTF-16 UnicodeEncoding
UTF-32 UTF32Encoding

有关详细信息,请参阅 .NET 中的字符编码

另请参阅

适用于

ToCharArray()

将此实例中的字符复制到 Unicode 字符数组。

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

返回

Char[]

元素为此实例的各字符的 Unicode 字符数组。 如果此实例是空字符串,则返回的数组为空且长度为零。

示例

以下示例调用 方法, ToCharArray 将字符串中的字符提取到字符数组。 然后,它显示原始字符串和数组中的元素。

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))
      Next
   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

注解

此方法将每个字符 (即 Char 字符串中的每个对象) 复制到字符数组。 复制的第一个字符位于返回的字符数组的索引零处;复制的最后一个字符位于索引 Array.Length - 1。

若要从字符数组中的字符创建字符串,请 String(Char[]) 调用 构造函数。

若要创建包含字符串中编码字符的字节数组,请实例化相应的 Encoding 对象并调用其 Encoding.GetBytes(String) 方法。 .NET 中提供的一些标准编码包括:

编码 对象
ASCII ASCIIEncoding
UTF-7 UTF7Encoding
UTF-8 UTF8Encoding
UTF-16 UnicodeEncoding
UTF-32 UTF32Encoding

有关详细信息,请参阅 .NET 中的字符编码

另请参阅

适用于