Simple, so do not ignore the actual application that \ r \ n and \ n programmers should understand. Do not ignore the programmer

Source: Internet
Author: User

Simple, so do not ignore the actual application that \ r \ n and \ n programmers should understand. Do not ignore the programmer

As we all know, \ r is a carriage return and \ n is a linefeed.

For historical reasons, the line break in windows is \ r \ n)

The linefeed in open source or open standards such as linux and html is \ n.

 

The reason for recording this note is:

When textarea contains line breaks, an \ r symbol is automatically added after receiving them in C. Assume that this field is named Name nvarchar (50). Because an \ r symbol is automatically added, it is passed in the front-end verification, but an error is returned when it is saved to the database. Because the current length is 51.

If you are interested, perform the following test:

Enter character 1 in <textarea id = "name"> </textarea>, press enter, and enter 2

Get 3 in JS detection length, $ ('# name'). val (). length = 3 is true

In C #, the length is 4, and name. length = 4 is true.

Because the line breaks are invisible characters, you can use the replace (/\ n/g, ''). length method to detect them, as shown in JS:

We can see that textarea does not have the \ r symbol.

Look at C # Again

sqlParams.ElementAt(1).Value.ToString().Replace("\r\n","").Length2sqlParams.ElementAt(1).Value.ToString().Length4

We can see that the linefeed in C # has \ r.

This problem occurs when I come to the door.

Solution:

Remove \ r, replace ("\ r ","");

Why?

Even if you do not remove \ r, when the read data is re-displayed in textarea, The linefeed is changed to \ n without \ r.

All these changes are made by operating systems and browsers. Therefore, it is reasonable to remove \ r before saving the database.

When displaying read-only text in html, do not forget that the line break of html is <br/>

Therefore, replace ('\ n',' <br/> ') is required before the read-only text is displayed ').

 

PS:

Why \ r \ n is reserved for windows as a line break:

Why is the return key? Have you ever thought about it? It literally means the car that is going back.

For the first printer, after each line is printed, before printing the second line, the inkjet things need to first return to the first line of this line. This is called a carriage return and then jump to the next line, this is called line feed. \ R \ n is the carriage return and line feed. The line feed is always called the carriage return line to keep and continue the tradition. Only the technology on the windows platform is like this. In all linux and other open-source standards, \ n is used as a line break.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.