C# ToCharArray() Method


The ToCharArray() method in C# is used to copy the characters in this instance to a Unicode character array.

Syntax

The syntax is as follows -

public char[] ToCharArray ();
public char[] ToCharArray (int begnIndex, int len);

Above, begnIndex is the beginning position of a substring in this instance. The len is the length of the substring in this instance.

Example

Let us now see an example -

 Live Demo

using System;
public class Demo {
   public static void Main(String[] args) {
      string str1 = "Notebook";
      string str2 = "Ultrabook";
      char[] arr1 = str1.ToCharArray();
      char[] arr2 = str2.ToCharArray();
      Console.WriteLine("String1 = "+str1);
      Console.WriteLine("String1 ToUpperInvariant = "+str1.ToUpperInvariant());
      Console.WriteLine("String1 Substring from index4 = " + str1.Substring(4, 4));
      Console.Write("Character array...String1 =");
      for (int i = 0; i < arr1.Length; i++)
         Console.Write(" " + arr1[i]);
      Console.WriteLine("

String2 = "+str2);       Console.WriteLine("String2 ToUpperInvariant = "+str2.ToLowerInvariant());       Console.WriteLine("String2 Substring from index2 = " + str2.Substring(0, 5));       Console.Write("Character array...String2 =");       for (int i = 0; i < arr2.Length; i++)          Console.Write(" " + arr2[i]);    } }

Output

String1 = Notebook
String1 ToUpperInvariant = NOTEBOOK
String1 Substring from index4 = book
Character array...String1 = N o t e b o o k
String2 = Ultrabook
String2 ToUpperInvariant = ultrabook
String2 Substring from index2 = Ultra
Character array...String2 = U l t r a b o o k

Example

 Live Demo

using System;
public class Demo {
   public static void Main(String[] args) {
      string str1 = "Welcome!";
      string str2 = "Thisisit!";
      char[] arr1 = str1.ToCharArray(3,2);
      char[] arr2 = str2.ToCharArray(2,2);
      Console.WriteLine("String1 = "+str1);
      Console.WriteLine("String1 ToUpperInvariant = "+str1.ToUpperInvariant());
      Console.WriteLine("String1 Substring from index4 = " + str1.Substring(4, 4));
      Console.Write("Character array...String1 =");
      for (int i = 0; i < arr1.Length; i++)
         Console.Write(" " + arr1[i]);
      Console.WriteLine("

String2 = "+str2);       Console.WriteLine("String2 ToUpperInvariant = "+str2.ToLowerInvariant());       Console.WriteLine("String2 Substring from index2 = " + str2.Substring(0, 5));       Console.Write("Character array...String2 =");       for (int i = 0; i < arr2.Length; i++)          Console.Write(" " + arr2[i]);    } }

Output

This will produce the following output -

String1 = Welcome!
String1 ToUpperInvariant = WELCOME!
String1 Substring from index4 = ome!
Character array...String1 = c o
String2 = Thisisit!
String2 ToUpperInvariant = thisisit!
String2 Substring from index2 = Thisi
Character array...String2 = i s

Updated on: 03-Dec-2019

431 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements