Matt
Matt

Reputation: 3970

Parsing a text file, handling a new line?

I'm trying to write a simple program which takes a textfile, sets all characters to lowercase and removes all punctuation. My problem is that when there is a carriage return (I believe that's what it is called) and a new line, the space is removed.

i.e.

This is a
test sentence

Becomes

This is atestsentence

The last word of the first line and the first word of the next line are joined.

This is my code:

public static void ParseDocument(String FilePath, String title)
    {
        StreamReader reader = new StreamReader(FilePath);
        StreamWriter writer = new StreamWriter("C:/Users/Matt/Documents/"+title+".txt");

        int i;
        char previous=' ';
        while ((i = reader.Read())>-1)
        {
            char c = Convert.ToChar(i);
            if (Char.IsLetter(c) | ((c==' ') & reader.Peek()!=' ') | ((c==' ') & (previous!=' ')))
            {
                c = Char.ToLower(c);
                writer.Write(c);                    
            }
            previous = c;

        }

        reader.Close();
        writer.Close();
    }

It's a simple problem, but I can't think of a way of checking for a new line to insert a space. Any help is greatly appreciated.

Upvotes: 0

Views: 1164

Answers (2)

Henk Holterman
Henk Holterman

Reputation: 273244

Depends a little on how you want to treat empty lines but this might work :

 char c = Convert.ToChar(i);

 if (c == '\n')  
    c = ' ';     // pretend \n == ' ' and keep ignoring \r

 if (Char.IsLetter(c) | ((c==' ') & reader.Peek()!=' ') | ((c==' ') & (previous!=' ')))
 {
    ...

I do hope this is an exercise, in normal practice you would read a Text file with System.IO.File.ReadAllLines() or System.IO.File.ReadLines()

Upvotes: 2

elrado
elrado

Reputation: 5272

Try

myString.Replace(Environment.NewLine, "replacement text")

Replace Line Breaks in a String C#

Upvotes: 0

Related Questions