none
How to delete all empty lines from c sharp source code?

Answers

  • Find&Replace -> use regular expressions

    Replace \n\n by nothing.

    Repeat until there are no more blank lines. May make your source unreadable.

    • Proposed as answer by vijaykamat Tuesday, December 14, 2010 9:33 AM
    • Marked as answer by Iceweasel Tuesday, December 14, 2010 3:26 PM
    Monday, December 13, 2010 5:20 PM
  • Goto menu Project->Find & Replace->Quick Replace; check the last check box "Use" select Regular Expressions from combo box bellow.

    Type \n\n in Find What box

    and in Replace box leave it empty..

    • Proposed as answer by Prashant Khandelwal Tuesday, December 14, 2010 9:43 AM
    • Marked as answer by Iceweasel Tuesday, December 14, 2010 3:26 PM
    Tuesday, December 14, 2010 9:33 AM

All replies

  • What is the source code look like?
    Monday, December 13, 2010 4:43 PM
  • Did you had in mind something like that:

        private static void myMethod()
        {
          string msg = "This is some text\r\nand this too\r\n\r\nanother line\r\n\r\nThis is the last one...\r\n\r\n.. or not...\r\nIt is.END";
          string[] array = msg.Split(new string[] { "\r\n" }, StringSplitOptions.None);
          List<string> list = new List<string>();
          for (int i = 0; i < array.Length; i++)
          {
            if (array[i].Length > 0)
              list.Add(array[i]);
          }
        }
    

    ... if not, please tell us some more about the issue.

    Mitja

    Monday, December 13, 2010 4:50 PM
  • private static void myMethod()
      {
       string msg = "This is some text";
    
    
       string[] array = msg.Split(new string[] { "\r\n" }, StringSplitOptions.None);
    
    
       List<string> list = new List<string>();
    
    
       for (int i = 0; i < array.Length; i++)
       {
    
    
        if (array[i].Length > 0)
         list.Add(array[i]);
    
    
       }
    
    
      }
    
    

    Expected output:

      private static void myMethod()
      {
       string msg = "This is some text";
       string[] array = msg.Split(new string[] { "\r\n" }, StringSplitOptions.None);
       List<string> list = new List<string>();
       for (int i = 0; i < array.Length; i++)
       {
        if (array[i].Length > 0)
         list.Add(array[i]);
       }
      }
    
    

    Monday, December 13, 2010 4:53 PM
  • Sorry, what did you want to tell? I missed the point in our last post.
    Monday, December 13, 2010 5:07 PM
  • See the last post from me.

    There are empty lines or new lines.

    I want to delete those empty lines or new lines.

    It's a Visual Studio IDE related question.

    Is there any option in Visual Studio IDE to remove those empty lines or new lines from source code/program?

    Monday, December 13, 2010 5:10 PM
  • Find&Replace -> use regular expressions

    Replace \n\n by nothing.

    Repeat until there are no more blank lines. May make your source unreadable.

    • Proposed as answer by vijaykamat Tuesday, December 14, 2010 9:33 AM
    • Marked as answer by Iceweasel Tuesday, December 14, 2010 3:26 PM
    Monday, December 13, 2010 5:20 PM
  • LOL - I have finlly found out what is he taling aobut - about the lines of rthe code (not of some string in the project).

    My code would do it, just put all the text into some text file (or something) and use StreamReader to read it from the dics, and then use the code I gave you. Even Louis`s code its good, where you use Find and Replace method.

     

    Monday, December 13, 2010 5:29 PM
  • Find&Replace -> use regular expressions

    Replace \n\n by nothing.

    Repeat until there are no more blank lines. May make your source unreadable.

    Can you please post complete regular expression?

    Can't understand how to use regular expression to delete empty lines or newlines?

    Tuesday, December 14, 2010 9:20 AM
  • Goto menu Project->Find & Replace->Quick Replace; check the last check box "Use" select Regular Expressions from combo box bellow.

    Type \n\n in Find What box

    and in Replace box leave it empty..

    • Proposed as answer by Prashant Khandelwal Tuesday, December 14, 2010 9:43 AM
    • Marked as answer by Iceweasel Tuesday, December 14, 2010 3:26 PM
    Tuesday, December 14, 2010 9:33 AM
  • Use the regex option in find and replace. However the regex in VS find and replace has an unusual syntax (see http://msdn.microsoft.com/en-us/library/2k3te2cs(v=vs.80).aspx)

    To remove blank lines replace:

    ^:Wh*\n 

    with nothing. 


    • Proposed as answer by Saagar Soni Wednesday, January 22, 2014 6:23 AM
    Friday, March 16, 2012 2:54 PM
    1. go to Find and replace
    2. tick 'Use regular expression'
    3. Put ^\n in find what textbox
    • Proposed as answer by Parvej Solkar Saturday, October 20, 2012 5:47 AM
    Saturday, October 20, 2012 5:47 AM
  • very correct code....for replacing blank lines in code

    Wednesday, January 22, 2014 6:23 AM