[.net] Coverting a String into a Char Array

Started by
3 comments, last by Bob Janova 17 years, 11 months ago
Hi, I've read a file into a stream. Now i want to convert the stream in to a Char Array so that i can work on the values at individual indexes in the array. I've tried everything but I still cannot convert the stream into an array. Please Help. Thank you.
Advertisement
In the topic title you mentioned convertiong a sting to a char array. in your post you want to convert a stream type to a char array, that makes no sense.
the easiest way to read a file into a string is:

System.IO.StreamReader sr = new System.IO.StreamReader("file"); //create a StreamReader object, that opens a file or a stream for reading

string str;
sr.ReadToEnd(str); //read the whole content (from current read position) of the stream into the string
sr.Close(); //close the stream when finished reading data
char ch = str[0]; //access the elements of the string (characters) with the index operator. of course you cannot index further than the length of the string (str.Length)

ps.: a string is an array of chars ;) why would you convert it?
"Knowledge is no more expensive than ignorance, and at least as satisfying." -Barrin
I'm sorry, I wanted to write that I've copied a stream in to string and now wanted to convert it to a Char Array. Anyway,it's resolved now. Thanks for the help.
There is always the System.String::ToCharArray() method...

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

For future reference for people who might search this question:
char[] cvec = myString.ToCharArray();string str = new String(cvec);

This topic is closed to new replies.

Advertisement