Ask a questionAsk a question
 

AnswerHow do you test for newline

  • Wednesday, October 21, 2009 7:47 PMj2associates Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello all,

    How do you put the newline character into a pattern in .Net? \n does not seem to work. Thanks in advance for any ideas and/or suggestions!

Answers

  • Wednesday, October 21, 2009 10:25 PMAhmad Mageed Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Use "\r\n" for non-Unix systems. To make it system neutral, you can use the framework provided Environment.NewLine property in the pattern as well.

    Example:

    string input = "new4" + Environment.NewLine + "2line";
    Console.WriteLine("Text: " + input);
    Console.WriteLine("Result: " + Regex.Match(input, @"\d\r\n\d").Value);
    


    Document my code? Why do you think it's called "code"?
  • Thursday, October 22, 2009 8:11 AMJialiang Ge [MSFT]MSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hello

    \r stands for carriage return (0x0D) and \n stands for line feed (0x0A). On Windows systems, text files use \r\n to terminate lines, while UNIX text files use \n.

    Regards,
    Jialiang Ge

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

All Replies