C# | ToCharArray() Method Last Updated : 31 Jan, 2019 Comments Improve Suggest changes Like Article Like Report In C#, ToCharArray() is a string method. This method is used to copy the characters from a specified string in the current instance to a Unicode character array or the characters of a specified substring in the current instance to a Unicode character array. This method can be overloaded by changing the number of arguments passed to it. Syntax: public char[] ToCharArray() or public char[] ToCharArray(int startIndex, int length) Explanation: public char[] ToCharArray() method will take no parameter. This method will return character array. Character array elements will contain the individual characters of the string. So basically this method will copy each character of a string to the character array. The first character of the string will be copied at index zero of the character array and the last character will be copied at index Array.Length – 1. When we create a string from the characters in a character array, then it will call String(Char[]) constructor. public char[] ToCharArray(int startIndex, int length) method will take two parameters startIndex which is used to specify the starting position of the substring and its type is System.Int32. Another parameter is length which is used to specify the length of the substring and its type is System.Int32. The startIndex parameter is zero-based, means the index of the first character in the string instance can be zero. Important Points: public char[] ToCharArray(int startIndex, int length) method can give the exception ArgumentOutOfRangeException if startIndex or length is less than zero or (startIndex + length) is greater than the length of current string instance. If the specified length is zero then the returned array will be empty and will have zero length. If current or this instance is null or an empty string ("") then the returned array will be empty will have length zero. Below are the programs to demonstrate the above Methods : Program 1: To illustrate the ToCharArray() Method : csharp // C# program to demonstrate // ToCharArray() method using System; class Geeks { // Main Method public static void Main() { String str = "GeeksForGeeks"; // copy the string str to chars // character array & it will start // copy from 'G' to 's', i.e. // beginning to ending of string char[] chars = str.ToCharArray(); Console.WriteLine("String: " + str); Console.Write("Character array :"); // to display the resulted character array for (int i = 0; i < chars.Length; i++) Console.Write(" " + chars[i]); } } Output: String: GeeksForGeeks Character array : G e e k s F o r G e e k s Program 2:To illustrate the ToCharArray(int startIndex, int length) Method : csharp // C# program to demonstrate // ToCharArray(int startIndex, int length) // method using System; class Geeks { // Main Method public static void Main() { String str = "GeeksForGeeks"; // copy the string str to chars // character array & it will // start copy from 'F' to 'r' i.e. // copy from startindex of string // is 5 upto (startindex+length-1) i.e. // 5 + (3-1) has to copy char[] chars1 = str.ToCharArray(5, 3); Console.WriteLine("String: " + str); Console.WriteLine("\nCharacter array 1:"); // to display the resulted character array for (int i = 0; i < chars1.Length; i++) Console.WriteLine("Index " + i + " : " + chars1[i]); // copy the string str to chars // character array & it will // start copy from 'F' to 'k' i.e. // copy from startindex of string // 5 upto (startindex+length-1) i.e. // 5 + (7-1) char[] chars2 = str.ToCharArray(5, 7); Console.Write("\nCharacter array 2:"); // to display the resulted character // array using foreach loop foreach(char c in chars2) Console.Write(" " + c); } } Output: String: GeeksForGeeks Character array 1: Index 0 : F Index 1 : o Index 2 : r Character array 2: F o r G e e k References : https://msdn.microsoft.com/en-us/library/system.string.tochararray1 https://msdn.microsoft.com/en-us/library/system.string.tochararray2 Comment More infoAdvertise with us Next Article C# | ToCharArray() Method M Mithun Kumar Follow Improve Article Tags : Misc C# CSharp-method CSharp-string Practice Tags : Misc Similar Reads C# | Convert.ToBase64CharArray() Method | Set-1 This method is used to convert a subset of an 8-bit unsigned integer array to an equivalent subset of a Unicode character array encoded with base-64 digits. Parameters specify the subsets as offsets in the input and output arrays, and the number of elements in the input array to convert. Syntax: pub 5 min read C# | BitConverter.ToChar() Method This method is used to return a Unicode character converted from two bytes at a specified position in a byte array.Syntax: public static char ToChar (byte[] value, int startIndex); Parameters: value: It is an array. startIndex: it is the starting position within value. Return Value: This method retu 6 min read Array setChar() method in Java The java.lang.reflect.Array.setChar() is an inbuilt method in Java and is used to change a specified char value to a specified index of a given object array. Syntax: Array.setChar(Object []array, int index, char value) Parameter: This method takes three parameters: array: This is an array of type Ob 3 min read C# String Split() Method In C#, Split() is a string class method used to divide a string into an array of substrings. The Split() method divides a string into an array of substrings using specified delimiters.The delimiters can be:A character for example comma(,) or underscore(-). An array of characters char[] or an array o 6 min read C# String CopyTo() Method In C#, the CopyTo() is a method of String Class. It is used to copy a specified number of characters from a specified position in the string and it copies the characters of this string into an array of Unicode characters.Example 1: Using the CopyTo() method to copy characters from a string to a char 2 min read StringBuilder.CopyTo Method in C# This method is used to copy the characters from a specified segment of this instance to a specified segment of a destination Char array. Syntax: public void CopyTo (int sourceIndex, char[] destination, int destinationIndex, int count); Parameters: sourceIndex: It is the starting position in this ins 2 min read CharBuffer wrap() method in Java wrap(char[ ] array) The wrap() method of java.nio.CharBuffer Class is used to wrap a character array into a buffer. The new buffer will be backed by the given char array. As a consequence, any modifications to the buffer will cause the array to be modified and vice versa. The new buffer's capacity i 4 min read strrchr() in C The strrchr() function in C locates the last occurrence of a character in a string and returns a pointer to it. It is a standard library function defined inside <string.h> header file. Syntax : char* strrchr( char* str, int chr ); Parameter: str: specifies the pointer to the null-terminated st 2 min read C# | Array.AsReadOnly(T[]) Method This method is used to return a read-only wrapper for the specified array. Syntax: public static System.Collections.ObjectModel. ReadOnlyCollection<T> AsReadOnly<T> (T[] array); Here, T is the type of element of the array. Return Value: This method return the a read-only ReadOnlyCollecti 2 min read strupr() function in c The strupr( ) function is used to converts a given string to uppercase. Syntax: char *strupr(char *str); Parameter: str: This represents the given string which we want to convert into uppercase. Returns: It returns the modified string obtained after converting the characters of the given string str 1 min read Like