简体   繁体   中英

How to carriage return C# string without \r\n?

This is my problem.

A user can enter text into a text area in the browser. Which is then emailed out to users. What I want to know is that how do I handle carriage return? If I enter \\r\\n for carriage return, the email (which is a plain text email) has actual \\r\\n in it.

In other words:

On the SQL server end

Case 1: if I do this before the email gets sent (notice the line break after line 1)

update emails
set
body='line 1
line 2'
where
id=100

the email goes out correctly

Case 2:

 update emails
 set
 body='line 1'+char(13) + char(10) +'line 2'
 where
 id=100

This email also goes out correctly

Case 3: However if I do this

update emails
set
body='line 1 \r\n line 2',
where
id=100

the email would have the actual text \\r\\n in it.

How do I simulate case 1/2 through c# ?

SQL literals (at least those in SQL Server) do not support such escape sequences (although you can just hit enter within the string literal so that it spans multiple lines). See this answer for some alternatives if writing it as an SQL string is a requirement.

If running the SQL programmatically from C#, use parameters which will handle this just fine:

sqlCommand.CommandText = "update emails set body=@body where id=@id"
sqlCommand.Parameters.AddWithValue("@body", "line 1 \r\n line2");

Note that the handling of the string literal (and conversion of the \\r and \\n character escape sequences) happens in C# and the value (with CR and LF characters ) is passed to SQL.

If the above didn't address the problem, keep reading.


4.10.13 The textarea element :

For historical reasons, the element's value is normalised in three different ways for three different purposes. The raw value is the value as it was originally set. It is not normalized. The API value is the value used in the value IDL attribute. It is normalized so that line breaks use "LF" (U+000A) characters. Finally, there is the form submission value. [Upon form submission the textarea] is normalized so that line breaks use U+000D CARRIAGE RETURN U+000A LINE FEED (CRLF) character pairs , and in addition, if necessary given the element's wrap attribute, additional line breaks are inserted to wrap the text at the given width.

Note that CR and LF represent characters and not the two-character sequence of \\ followed by either the r or n characters - this form is often found in string literals . If it appears as such then something is doing the incorrect conversion and putting (or leaving ) the \\ there. Or, perhaps there is some misguided "add slashes" hack somewhere?

As pointed out, while URL decode is likely wrong, it won't directly do this conversion. However, if the conversion happened previously before being "URL Encoded", then it will (correctly) decode to (incorrect) values.

In either case, it's a bug . So find out where the incorrect data conversion is introduced and fix it (attach a debugger and/or monitor the network traffic for clues) - the required information to isolate where is simply not present in the post.

使用任何c#的字符串替换方法将"\\\\r\\\\n"替换为"\\r\\n" ,这应该可以解决。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM