954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Copy string into an array

I have a string "Hello". How can I copy this string into an array in words e.g 'H', 'e', 'l', 'l', 'o'.

Thanks in advance

neelu421
Newbie Poster
1 post since Dec 2005
Reputation Points: 10
Solved Threads: 0
 

I have a string "Hello". How can I copy this string into an array in words e.g 'H', 'e', 'l', 'l', 'o'.

Thanks in advance

a string is nothing more then an array of chars, so if you want to split up the strings letters into a different string array seperatly you could do something like this:

string myString = "blah";
			string[] myArray= new string[myString.Length];
			for(int i = 0; i<myString.Length; i++){

				myArray[i] = myString[i].ToString();
			}
plazmo
Posting Whiz in Training
207 posts since Aug 2005
Reputation Points: 23
Solved Threads: 16
 

As has been mentioned, a string IS an array, of characters. You can use it like an array already, so there isn't really a need to convert it explicity. Perhaps if we knew what you were trying to do, we could give better advice.

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

string theString = "hello";
char[] theStringAsArray = s.ToCharArray();

f1 fan
Posting Whiz in Training
279 posts since Jan 2006
Reputation Points: 26
Solved Threads: 11
 
string theString = "hello"; char[] theStringAsArray = s.ToCharArray();


yes of course, some how i forgot about that

plazmo
Posting Whiz in Training
207 posts since Aug 2005
Reputation Points: 23
Solved Threads: 16
 

the simplest solution is that:
string str="hello";
char[] ch = str.ToArray();
want to knpw that sting is totally converted into char array so used foreach loop because it iterate all elements in array..
foreach(char a in ch)
{
Console.WriteLine(a);//used console application for output
}

osama.rahat
Newbie Poster
1 post since Feb 2010
Reputation Points: 6
Solved Threads: 0
 

The two main methods of accessing the characters of a string have been mentioned here. I thought i'd also mention the string.split function. It won't split a word into characters, but it can be used to split a string into its composite substrings. I put all the methods together in a console app so you could see the similarities/differences:

class Program
    {
        static void Main(string[] args)
        {
            string helloString = "Hello World";
            Console.WriteLine(helloString[0]); //reference char by index
            foreach (char ch in helloString) //iterate through char collection
            {
                Console.Write(ch);
            }
            Console.WriteLine();

            char[] helloChars = helloString.ToCharArray(); //explicitly convert to char array
            Console.WriteLine(helloChars[0]); //ouput content of array position
            foreach (char ch in helloChars) //iterate through char array
            {
                Console.Write(ch);
            }
            Console.WriteLine();

            char[] seperators = { ' ' }; //declare array of characters that will mark seperation of 'words'
            string[] helloStrings = helloString.Split(seperators); //split string into array of substrings
            Console.WriteLine(helloStrings[0]);//output first substring
            Console.WriteLine(helloStrings[1]);//output second substring

            Console.ReadKey();
        }
    }


The first two blocks of code really highlight what has been said here. Whilst you can convert a string to an array of characters, it is in fact already an array of characters. You can access each by index, and iterate through them the same way.

Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: