CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: shorten it up

  1. #1
    Join Date
    Nov 2007
    Posts
    74

    Post shorten it up

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: shorten it up

    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
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Apr 2008
    Location
    Cleveland, OH USA
    Posts
    20

    Re: shorten it up

    I used a regular expression to find all occurences of 3 or more consecutive returns and replace it with only two returns.
    Code:
                //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:
    Code:
    One return
    Two Returns
    
    Three Returns
    
    
    Four Returns
    
    
    
    End
    Into this:
    Code:
    One return
    Two Returns
    
    Three Returns
    
    Four Returns
    
    End
    Hope that helps.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured