Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
C# Program to Count Punctuation Characters in a String
Next article icon

Convert String to Character Array in C#

Last Updated : 06 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C#, converting a string to a character array is a straightforward process that allows you to manipulate individual characters within the string. This conversion can be useful for various operations, such as character manipulation, processing text, or analyzing strings.

Example

Input: "Hello, Geeks!"; Output: H, e, l, l, o, , ,  , G, e, e, k, s, ! Explanation: The string "Hello, Geeks!" is converted into a character array, resulting in the individual characters being displayed, including spaces and punctuation.

Syntax

char[] charArray = new char[input.Length]; for (int i = 0; i < input.Length; i++) {                                                                     # Naive Method     charArray[i] = input[i]; }  char[] charArray = stringVariable.ToCharArray();                                           # ToCharArray() Method  char[] charArray = input.Select(c => c).ToArray();                                          # LINQ Method 

Table of Content

  • Naive Method
  • Using ToCharArray() Method
  • Using LINQ Method

Naive Method

The naive method involves manually creating a character array and populating it with each character of the string.

Syntax

char[] charArray = new char[input.Length]; for (int i = 0; i < input.Length; i++) {     charArray[i] = input[i]; }
C#
using System;  public class GFG {     static public void Main() {         string input = "Hello, Geeks!";         char[] charArray = new char[input.Length];          // Manually populating the character array         for (int i = 0; i < input.Length; i++) {             charArray[i] = input[i];         }          Console.WriteLine(string.Join(" , ", charArray));     } } 

Output
H , e , l , l , o , , ,   , G , e , e , k , s , ! 

Using ToCharArray() Method

The ToCharArray() method converts a string into a character array.

Syntax

char[] charArray = stringVariable.ToCharArray();

Example

C#
using System;  public class GFG {     static public void Main() {         string input = "Hello, Geeks!";         char[] charArray = input.ToCharArray();         Console.WriteLine("Character Array: " + string.Join(" , ", charArray));     } } 

Output
Character Array: H , e , l , l , o , , ,   , G , e , e , k , s , ! 

Using LINQ Method

This method utilizes the LINQ library to convert a string into a character array.

Syntax

char[] charArray = input.Select(c => c).ToArray();

Example

C#
using System; using System.Linq;  public class GFG {     static public void Main() {         string input = "Hello, Geeks!";         char[] charArray = input.Select(c => c).ToArray();         Console.WriteLine("Character Array: " + string.Join(" , ", charArray));     } } 

Output
Character Array: H , e , l , l , o , , ,   , G , e , e , k , s , ! 

Conclusion

Converting a string to a character array in C# is essential for various string manipulations and analyses. The methods presented above provide effective ways to achieve this conversion, each serving different use cases. The naive method offers a straightforward approach, while the ToCharArray(), LINQ provide efficient alternatives for converting strings to character arrays.




Next Article
C# Program to Count Punctuation Characters in a String

S

SHUBHAMSINGH10
Improve
Article Tags :
  • C#
  • C# Programs
  • CSharp-string

Similar Reads

  • C# Program to Count Punctuation Characters in a String
    C# is a general-purpose, modern and object-oriented programming language pronounced as “C Sharp”. In this article we will see the C# program, to count punctuation characters in a given string.  Algorithm:First, create a string or get the string from the user.Declare a variable to count the number of
    1 min read
  • How to Get a Comma Separated String From an Array in C#?
    Given an array, now our task is to get a comma-separated string from the given array. So we can do this task using String.Join() method. This method concatenates the items of an array with the help of a comma separator between each item of the array. Syntax: String.Join(",", array_name)Where array_n
    2 min read
  • C# Program to Convert a Binary String to an Integer
    Given an binary string as input, we need to write a program to convert the binary string into equivalent integer. To convert an binary string to integer, we have to use Convert.ToInt32(String, Base/Int32) function to convert the values. The base of the binary is 2. Syntax: Convert.ToInt32(String, Ba
    2 min read
  • C# Program to Convert the Octal String to an Integer Number
    Given an octal number as input, we need to write a program to convert the given octal number into equivalent integer. To convert an octal string to an integer, we have to use Convert.ToInt32() function to convert the values. Examples: Input : 202 Output : 130 Input : 660 Output : 432 Convert the ite
    2 min read
  • C# Program to Split a String Collections into Groups
    Given a collection of strings and you are required to split them into groups using C#. The standard query operator contains GroupBy grouping operator using which we can split a collection of strings easily. The working of the GroupBy operator is similar to the SQL GroupBy clause. It is used to retur
    3 min read
  • C# | Copying the entire ArrayList to 1-D Array starting at the specified index
    ArrayList.CopyTo(Array, Int32) Method is used to copy the entire ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array. Syntax: public virtual void CopyTo (Array array, int arrayIndex); Parameters: array: It is the one-dimensional Array that is the dest
    4 min read
  • Different ways to convert String to Integer in C#
    In C#, we can convert a string to an integer using various methods. This process is common when dealing with user input or data parsing. In this article, we will learn Different ways to convert String to Integer in C#. Example:Input String: "10" Output String: 10 Explanation: The input string "10" i
    2 min read
  • C# Program for Converting Hexadecimal String to Integer
    Given an hexadecimal number as input, we need to write a program to convert the given hexadecimal number into equivalent integer. To convert an hexadecimal string to integer, we have to use Convert.ToInt32() function to convert the values. Syntax: Convert.ToInt32(input_string, Input_base); Here, inp
    2 min read
  • C# Program to Check a Specified Type is an Array or Not
    In C#, an array is a group of homogeneous elements that are referred to by a common name. So in this article, we will discuss how to check the variable is of array type or not. To do this task, we use the IsArray property of the Type class. This property is used to determine whether the specified ty
    2 min read
  • Convert a Character to the String in C#
    Given a character, the task is to character to the string in C#. Examples: Input : X = 'a' Output : string S = "a" Input : X = 'A' Output : string S = "A" Approach: The idea is to use ToString() method, the argument is the character and it returns string converting Unicode character to the string. /
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences