【发布时间】:2018-11-27 02:01:25
【问题描述】:
我有以下字符串:
This is the string to
test carriage return
using c#
上面的字符串在行尾有两个回车符。我必须对该字符串进行编码以传递给 WCF 服务,然后对该字符串进行解码以进行进一步处理。
//TO ENCODE
byte[] bytes = Encoding.Default.GetBytes(rawstring);
string encodedStr = Encoding.UTF8.GetString(bytes);
//TO DECODE
byte[] rawUtf8EncodedData = Encoding.Default.GetBytes(encodedstring);
string decodedStr = Encoding.UTF8.GetString(rawUtf8EncodedData);
编码后我得到以下字符串:
This is the string to\ntest carriage return\nusing c#
解码后我得到以下字符串:
This is the string to\ntest carriage return\nusing c#
问题是,由于某种原因,我需要回车,而不是解码后字符串中的 \n。我怎样才能做到这一点?以上字符串仅作说明,实际字符串中会有很多回车符和符号
【问题讨论】:
-
如果您在调试器中查看字符串,它将显示
\n而不是回车。尝试将字符串输出到控制台或日志文件,看看是否有回车。 -
我正在使用这个字符串来做一些逻辑,如果有 \n 就会失败。它需要回车才能工作。现在它失败了,因为 \n 存在于原始字符串中。
-
你在哪里“有以下字符串”?行尾可以用多种方式编码
-
刚刚测试过(可以肯定)。插入
\r\n或\r或\n,都作为原始返回。 [the] 字符串有两个回车符 是什么意思?你是从RichTextBox中提取这个字符串吗?。
标签: c# .net string carriage-return