c# - Retrieving data from the operating system and storing it in the program -
if text file stored lots of words in cat, rat, hat, bat stored in words.txt
, notepad how can retrieve each word separately , them stored in string array string []
myarray using filestream , streamreader
object. has stored myarray[0] = "cat"
, myarray[1] = "rat".
assuming words separated spaces (i.e. no punctuation, etc.), can read file string , split it:
string alltext = file.readalltext("words.txt"); string[] myarray = alltext.split( new [] {" ", environment.newline}, stringsplitoptions.removeemptyentries);
if reason absolutely have use filestream
, streamreader
, you'd write:
string alltext = null; using (filestream fs = new filestream(...)) // fill in file name , other params { using (streamreader sr = new streamreader(fs)) { alltext = sr.readtoend(); } } // string split here
now, if want take account punctuation , other special characters, that's more involved problem.
Comments
Post a Comment