Reading a text file word by word

后端 未结 9 950
梦谈多话
梦谈多话 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:30

    If you want to read it whitout spliting the string - for example lines are too long, so you might encounter OutOfMemoryException, you should do it like this (using streamreader):

    while (sr.Peek() >= 0)
    {
        c = (char)sr.Read();
        if (c.Equals(' ') || c.Equals('\t') || c.Equals('\n') || c.Equals('\r'))
        {
            break;
        }
        else
            word += c;
    }
    return word;
    

提交回复
热议问题