Home » .Net

How to convert a string into character array in C#.Net?

Converting string into character array in C#.Net - Here, we will learn how we can convert a string into character array using string.ToCharArray()?

Given a string and we have to convert into character array in C#.Net.

As we have discussed in character array in C# that strings are characters arrays are different in C#.Net, so it is required to convert string to character array, if we need to traverse of access character by character, that can be easily manageable using character array.

string.ToCharArray()

string.ToCharArray() method is a built in method provided by the .Net Framework, it is used to convert string to the character array.

Consider the program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {

			//string
			string str = "Hello, How are you?";
           
            int i = 0;
			
			//character array declaration
            char[] CH;
			
			//converting string to character array
            CH = str.ToCharArray();

			//printing character array character by character
            for (i = 0; i < CH.Length;i++ )
            {
                Console.Write(CH[i] + "");
            }

            Console.WriteLine();
        }
    }
}

Output

Hello, How are you?

In this program str is a string type variable, Using string.ToCharArray() method we copied the character of string str to CH.

And then, we are getting the length of the character array (CH) using Length property and printing character by character of CH (character array)






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.