Sign in to follow this  
Hot_Tamalle

[.net] Coverting a String into a Char Array

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
For future reference for people who might search this question:
char[] cvec = myString.ToCharArray();
string str = new String(cvec);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this