String.ToCharArray 方法

定义

将此实例中的字符复制到 Unicode 字符数组。Copies the characters in this instance to a Unicode character array.

重载

ToCharArray(Int32, Int32)

将此实例中的指定子字符串内的字符复制到 Unicode 字符数组。Copies the characters in a specified substring in this instance to a Unicode character array.

ToCharArray()

将此实例中的字符复制到 Unicode 字符数组。Copies the characters in this instance to a Unicode character array.

ToCharArray(Int32, Int32)

将此实例中的指定子字符串内的字符复制到 Unicode 字符数组。Copies the characters in a specified substring in this instance to a Unicode character array.

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

此实例内子字符串的起始位置。The starting position of a substring in this instance.

length
Int32

此实例内子字符串的长度。The length of the substring in this instance.

返回

Char[]

元素为此实例中从字符位置 length 开始的 startIndex 字符数的 Unicode 字符数组。A Unicode character array whose elements are the length number of characters in this instance starting from character position startIndex.

异常

startIndexlength 小于零。startIndex or length is less than zero.

-or- startIndexlength 大于此实例的长度。startIndex plus length is greater than the length of this instance.

示例

下面的示例将字符串中的子字符串转换为字符数组,然后枚举并显示该数组的元素。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:
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)
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
'

注解

此方法将一部分字符串中的字符复制到字符数组。This method copies the characters in a portion of a string to a character array. 若要从字符数组的字符范围中创建字符串,请调用 String(Char[], Int32, Int32) 构造函数。To create a string from a range of characters in a character array, call the String(Char[], Int32, Int32) constructor.

startIndex 参数是从零开始的。The startIndex parameter is zero-based. 也就是说,字符串实例中第一个字符的索引为零。That is, the index of the first character in the string instance is zero.

如果 length 为零,则返回的数组为空且长度为零。If length is zero, the returned array is empty and has a zero length. 如果此实例 null 或空字符串(""),则返回的数组为空且长度为零。If this instance is null or an empty string (""), the returned array is empty and has a zero length.

若要创建包含字符串的一部分中编码字符的字节数组,请实例化相应的 Encoding 对象并调用其 GetBytes(String, Int32, Int32, Byte[], Int32) 方法。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. .NET 中可用的一些标准编码包括:Some of the standard encodings available in .NET include:

编码Encoding 对象Object
ASCIIASCII ASCIIEncoding
UTF-7UTF-7 UTF7Encoding
UTF-8UTF-8 UTF8Encoding
UTF-16UTF-16 UnicodeEncoding
UTF-32UTF-32 UTF32Encoding

有关详细信息,请参阅.net 中的字符编码For more information, see Character Encoding in .NET.

另请参阅

ToCharArray()

将此实例中的字符复制到 Unicode 字符数组。Copies the characters in this instance to a Unicode character array.

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

返回

Char[]

元素为此实例的各字符的 Unicode 字符数组。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.

示例

下面的示例调用 ToCharArray 方法将字符串中的字符提取到字符数组中。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";
      var 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
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

下面的示例定义了一个字符串,其中包含在分隔字符串中用作分隔符的字符。The following example defines a string containing the characters that serve as delimiters in a delimited string. 然后,它调用 ToCharArray 方法来创建字符数组,该数组可传递到 Split(Char[]) 方法,以便将分隔的字符串分隔到其单独的子字符串中。It then calls the ToCharArray method to create a character array that can be passed to the Split(Char[]) method to separate the delimited string into its individual substrings.

using namespace System;
using namespace System::Collections;
int main()
{
   String^ delimStr = " ,.:";
   array<Char>^delimiter = delimStr->ToCharArray();
   String^ words = "one two,three:four.";
   array<String^>^split = nullptr;
   Console::WriteLine( "The delimiters are -{0}-", delimStr );
   for ( int x = 1; x <= 5; x++ )
   {
      split = words->Split( delimiter, x );
      Console::WriteLine( "\ncount = {0, 2} ..............", x );
      IEnumerator^ myEnum = split->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         String^ s = safe_cast<String^>(myEnum->Current);
         Console::WriteLine( "-{0}-", s );
      }

   }
}
// The example displays the following output:
//       The delimiters are - ,.:-
//       count =  1 ..............
//       -one two,three:four.-
//       count =  2 ..............
//       -one-
//       -two,three:four.-
//       count =  3 ..............
//       -one-
//       -two-
//       -three:four.-
//       count =  4 ..............
//       -one-
//       -two-
//       -three-
//       -four.-
//       count =  5 ..............
//       -one-
//       -two-
//       -three-
//       -four-
//       --
using System;

public class Example
{
   public static void Main() 
   {
      string delimStr = " ,.:";
      char [] delimiter = delimStr.ToCharArray();
      string words = "one two,three:four.";
      string [] split = null;

      Console.WriteLine("The delimiters are -{0}-", delimStr);
      for (int x = 1; x <= 5; x++) {
         split = words.Split(delimiter, x);
         Console.WriteLine("\ncount = {0,2} ..............", x);
         foreach (var s in split) {
             Console.WriteLine("-{0}-", s);
         }
      }
   }
}
// The example displays the following output:
//       The delimiters are - ,.:-
//       count =  1 ..............
//       -one two,three:four.-
//       count =  2 ..............
//       -one-
//       -two,three:four.-
//       count =  3 ..............
//       -one-
//       -two-
//       -three:four.-
//       count =  4 ..............
//       -one-
//       -two-
//       -three-
//       -four.-
//       count =  5 ..............
//       -one-
//       -two-
//       -three-
//       -four-
//       --
Public Class StringSplit2
   Public Shared Sub Main()
      
      Dim delimStr As String = " ,.:"
      Dim delimiter As Char() = delimStr.ToCharArray()
      Dim words As String = "one two,three:four."
      Dim split As String() = Nothing
      
      Console.WriteLine("The delimiters are -{0}-", delimStr)
      Dim x As Integer
      For x = 1 To 5
         split = words.Split(delimiter, x)
         Console.WriteLine(ControlChars.Cr + "count = {0,2} ..............", x)
         Dim s As String
         For Each s In  split
            Console.WriteLine("-{0}-", s)
         Next s
      Next x
   End Sub 
End Class 
' The example displays the following output:
'       The delimiters are - ,.:-
'       count =  1 ..............
'       -one two,three:four.-
'       count =  2 ..............
'       -one-
'       -two,three:four.-
'       count =  3 ..............
'       -one-
'       -two-
'       -three:four.-
'       count =  4 ..............
'       -one-
'       -two-
'       -three-
'       -four.-
'       count =  5 ..............
'       -one-
'       -two-
'       -three-
'       -four-
'       --

注解

此方法将字符串中的每个字符(即每个 Char 对象)复制到字符数组中。This method copies each character (that is, each Char object) in a string to a character array. 复制的第一个字符位于返回的字符数组的索引零处;复制的最后一个字符的索引 Array.Length 为-1。The first character copied is at index zero of the returned character array; the last character copied is at index Array.Length - 1.

若要从字符数组中的字符创建字符串,请调用 String(Char[]) 构造函数。To create a string from the characters in a character array, call the String(Char[]) constructor.

若要创建包含字符串中已编码字符的字节数组,请实例化相应的 Encoding 对象并调用其 Encoding.GetBytes(String) 方法。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. .NET 中可用的一些标准编码包括:Some of the standard encodings available in .NET include the following:

编码Encoding 对象Object
ASCIIASCII ASCIIEncoding
UTF-7UTF-7 UTF7Encoding
UTF-8UTF-8 UTF8Encoding
UTF-16UTF-16 UnicodeEncoding
UTF-32UTF-32 UTF32Encoding

有关详细信息,请参阅.net 中的字符编码For more information, see Character Encoding in .NET.

另请参阅

适用于