Reading a text file word by word

后端 未结 9 955
梦谈多话
梦谈多话 2020-12-10 18:58

I have a text file containing just lowercase letters and no punctuation except for spaces. I would like to know the best way of reading the file char by char, in a way that

9条回答
  •  臣服心动
    2020-12-10 19:26

    This is method that will split your words, while they are separated by space or more than 1 space (two spaces for example)/

    StreamReader streamReader = new StreamReader(filePath); //get the file
    string stringWithMultipleSpaces= streamReader.ReadToEnd(); //load file to string
    streamReader.Close();
    
    Regex r = new Regex(" +"); //specify delimiter (spaces)
    string [] words = r.Split(stringWithMultipleSpaces); //(convert string to array of words)
    
    foreach (String W in words)
    {
       MessageBox.Show(W);
    }
    

提交回复
热议问题