user2155926
user2155926

Reputation: 11

How to recognize line break in a database column's data?

Can anyone advise what is the ideal approach to recognize line break in a database column's data?

Using \r\n or chr(10)? What is the difference actually?

Does C# code need to do any special handling to split the lines or it will be recognized automatically?

Upvotes: 1

Views: 612

Answers (1)

davmos
davmos

Reputation: 9577

  1. \r is char(13) known as the "Carriage Return" character.
  2. \n is char(10) known as the "Line Feed" character.
  3. \r\n is char(13) + char(10) i.e. case 1 & 2 are concatenated.

Different operating systems use different combinations when reading or writing new lines in text data, the most common are:

  • Windows uses the combination in case 3, referred to as "Carriage Return Line Feed".
  • Unix and Unix-like systems use case 2, just the "Line Feed".

In C# you can handle all 3 cases by splitting the lines like this:

string[] lines = dbText.Split(new string[] { "\r\n", "\n", "\r" },
                              StringSplitOptions.None);

Other characters and combinations are used by other operating systems and this subject even has it's own Wikipedia page with way more detail than you'll probably ever need!

Upvotes: 3

Related Questions