加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 来自StreamReader.ReadLine()的字符串的编码是什么

发布时间:2020-12-15 08:14:20 所属栏目:百科 来源:网络整理
导读:首先,让我们看看代码: //The encoding of utf8.txt is UTF-8StreamReader reader = new StreamReader(@"C:utf8.txt",Encoding.UTF8,true);while (reader.Peek() 0){ //What is the encoding of lineFromTxtFile? string lineFromTxtFile = reader.ReadLin
首先,让我们看看代码:
//The encoding of utf8.txt is UTF-8
StreamReader reader = new StreamReader(@"C:utf8.txt",Encoding.UTF8,true);
while (reader.Peek() > 0)
{
    //What is the encoding of lineFromTxtFile?
    string lineFromTxtFile = reader.ReadLine();
}

正如乔尔在着名的article中所说:

If you have a string,in memory,in a file,or in an email message,you have to know what encoding it is in or you cannot interpret it or display it to users correctly.”

所以这里有我的问题:字符串lineFromTxtFile的编码是什么? UTF-8(因为它来自以UTF-8编码的文本文件)?或UTF-16(因为.NET中的字符串是“Unicode”(UTF-16))?

谢谢.

解决方法

.NET strings是Unicode.编码不起作用,然后直到你需要使用它.例如,如果您将其写入文件,则将指定输出编码.但是由于.NET通过库调用来处理你对字符串所做的一切,所以它在内存中的表示方式并不重要.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读