Remove empty lines in text using Visual Studio

后端 未结 11 1349
借酒劲吻你
借酒劲吻你 2020-12-12 08:36

How to remove empty lines in Visual Studio?

相关标签:
11条回答
  • 2020-12-12 08:50

    To remove double lines: ^:b*\n:b*\n replace with: \n

    Source: http://geekswithblogs.net/mnf/archive/2008/03/04/remove-empty-lines-in--text-using-visual-studio.aspx

    0 讨论(0)
  • 2020-12-12 09:00

    To remove two or more adjacent empty rows with VS2012 use this:

    ^(?([^\r\n])\s)*\r?$\r?\n^(?([^\r\n])\s)*\r?$\r?\n
    
    0 讨论(0)
  • 2020-12-12 09:01

    Install CodeMaid and hit Ctrl+M, Space to clean up the code. (It formats the code, like Format Document Ctrl+E, D, as well). You can clean up more files from Solution Explorer.

    0 讨论(0)
  • 2020-12-12 09:03

    It's very useful especially if you want to arrange or compare codes, thanks for the people who answer this question, I've got the answer from here and would like to share it with Stackoverflow:

    Visual Studio has ability to delete empty lines in replace operation using regular expressions.

    • Click Ctrl-H (quick replace)

    • Tick "Use Regular Expressions"

    • In Find specify ^$\n

    • In Replace box delete everything.

    • Click "Replace All"

    All empty lines will be deleted.

    Regular expression for empty line consist of

    Beginning of line ^

    End of line $

    Line break \n

    Note that normally in Windows an end of line indicated by 2 characters crlf - Carriage Return (CR, ASCII 13, \r) Line Feed (LF, ASCII 10, \n).

    A regex to remove blank lines that are/aren't really blank (i.e. they do/don't have spaces): ^:b*$\n

    To remove double lines: ^:b*\n:b*\n replace with: \n

    * for Visual Studio 2013 and above:*

    ^\s*$\n
    

    and for double lines:

    ^(?([^\r\n])\s)*\r?\n(?([^\r\n])\s)*\r?\n
    

    See the regular expression syntax updates for VS2012 and above in @lennart's answer below

    0 讨论(0)
  • 2020-12-12 09:04

    I'm using visual studio 2017, non of the above worked for me until I tried \n\r

    Steps:

    1. Ctrl + H (opens find and replace)
    2. Select use regular expression (Alt + E)
    3. Enter \n\r into the "Find..." input
    4. Press replace
    0 讨论(0)
  • 2020-12-12 09:05

    In VS 2012, the regex string to use to find and replace all blank lines is ^(?([^\r\n])\s)\r?\n

    0 讨论(0)
提交回复
热议问题