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[]

Unicode 字元陣列,其項目是從 length 字元位置起始的這個執行個體中的 startIndex 字元數。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- startIndex 加上 length 大於這個執行個體的長度。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 ObjectObject
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. 複製的第一個字元位於所傳回字元陣列的索引 0;最後複製的字元位於索引 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 ObjectObject
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.

另請參閱

適用於