Click to See Complete Forum and Search --> : shorten it up


Thu
April 11th, 2008, 10:34 PM
I have a document but there are many carriage returns in each paragraph
I'd like to delete some of them for the paragraph to be shorter

For example if there are N carriage returns then I'd like to delete N-1 starting from the second \n

Any help is appriciated.

dglienna
April 11th, 2008, 11:41 PM
Use .Replace() to change all 3 <ret>'s into 2 for ALL occurences

Then you will have all single space, except when there were more than 3

Then, do the same thing to replace a 2 <ret>'s into 1 for single spaced

mckee.kyle
April 12th, 2008, 10:57 AM
I used a regular expression to find all occurences of 3 or more consecutive returns and replace it with only two returns.

//I'm pulling the start text out of a textbox.
string startText = textBox1.Text;

//This regular expression matches 3 or more line returns.
Regex MultipleLineReturns = new Regex(@"(\r\n){3,}");

//Replace all occurrences of 3 or more line returns with 2 line returns.
//And wing it back in the text box.
textBox1.Text = MultipleLineReturns.Replace(startText, "\r\n\r\n");


That will turn this:

One return
Two Returns

Three Returns


Four Returns



End

Into this:

One return
Two Returns

Three Returns

Four Returns

End


Hope that helps.