1. Definition
Namespace:
System
Assembly:
Copy the characters from this instance to the Unicode character array.
2. Reload
ToCharArray(Int32, Int32) | Copy the characters in the specified substring in this instance to the Unicode character array. |
ToCharArray() | Copy the characters from this instance to the Unicode character array. |
3. ToCharArray()
Copy the characters from this instance to the Unicode character array.
public char[] ToCharArray ();
Return to Char[]
The element is a Unicode character array of each character of this instance. If this instance is an empty string, the returned array is empty and has zero length.
1. Example
Call method ToCharArray Extracts the characters from the string to the character array and displays the elements in the array.
// () namespace ConsoleApp4 { public class Example { public static void Main() { string str = "AaBbCcDd"; char[] chars = (); ("Original string: {0}", str); ("Character array:"); for (int i = 0; i < ; i++) { (" {0}: {1}", i, chars[i]); } } } } //Run result:/* Original string: AaBbCcDd Character array: 0: A 1: a 2: B 3: b 4: C 5: c 6: D 7: d */
2. Standard code
coding | Object |
ASCII | ASCIIEncoding |
UTF-7 | UTF7Encoding |
UTF-8 | UTF8Encoding |
UTF-16 | UnicodeEncoding |
UTF-32 | UTF32Encoding |
3. Others
To create a string from characters in a character array, call the constructor String(Char[]).
To create a byte array containing encoded characters in a string, instantiate the corresponding Encoding object and call its (String) method.
4. ToCharArray(Int32, Int32)ToCharArray(Int32, Int32)
Copy the characters in the specified substring in this instance to the Unicode character array.
public char[] ToCharArray (int startIndex, int length);
parameter
startIndex
Int32
The start position of the substring in this instance.
length
Int32
The length of the substring in this instance.
return
Char[]
The element is a Unicode character array of startIndex characters starting from the character position length in this instance.
exception
ArgumentOutOfRangeException
startIndex or length is less than zero.
or
startIndex plus length is greater than the length of this instance.
1. Example
// (Int32, Int32) namespace ConsoleApp5 { class Sample { public static void Main() { string str = "012wxyz789"; char[] array; array = (3, 4); //4 characters starting from index=3 ("The letters in '{0}' are: '", str); (array); ("'"); ("Each letter in '{0}' is:", str); foreach (char c in array) (c); } } } /* Running results: The letters in '012wxyz789' are: 'wxyz' Each letter in '012wxyz789' is: w x y z */
2. Others
ToCharArray(Int32, Int32) copies part of the string into a character array. To create a string from a character range in a character array, call the constructor String(Char[], Int32, Int32) . Parameter startIndex starts from zero. The index of the first character in the string instance is zero.
If length is zero, the returned array is empty and the length is zero. If this instance is null or an empty string ("") , the returned array is empty and has zero length.
To create a byte array containing partially encoded characters of the string, instantiate the corresponding Encoding object and call its GetBytes(String, Int32, Int32, Byte[], Int32) method.
This is the end of this article about the detailed explanation of the use of methods in C#. For more related C# content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!