[.net] Coverting a String into a Char Array
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.
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?
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?
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.
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
Popular Topics
Advertisement