【问题标题】:Why is my string output printing out weird characters?为什么我的字符串输出打印出奇怪的字符?
【发布时间】:2017-08-27 10:43:07
【问题描述】:

所以我有这种方法可以从文本文件中打印几行到列表视图,但无论何时它都会使用诸如“Å å , Ä ä, Ö ö” 之类的字符,并使它们成为奇怪的符号,例如三角形和其他东西。 我尝试将它们转换为 UTF-8 字符,但它给了我相同的输出。

private void btnSort_Click(object sender, EventArgs e)
{
    //Convert to utf8

    string[] Accounts = File.ReadAllLines(filePath); // if null do something

    foreach (string account in Accounts)
    {
        ListViewItem lvi = new ListViewItem(account);
        listView1.Items.Add(lvi);
    }

}

这是文本文件列表的样子

Gräsklippare@hotmail.com
Åkerström@gmail.com

是的,就是这样。

【问题讨论】:

  • File.ReadAllLines 有一个需要编码的重载。使用该文件使用的任何编码(显然不是 UTF8)调用它
  • 找到了!谢谢楼主!

标签: c# arrays listview encoding utf-8


【解决方案1】:

File.ReadAllLines 有一个需要编码的重载。使用该文件使用的任何编码调用它(显然不是 UTF8)

【讨论】:

    【解决方案2】:

    正如@KevinGosse 指出的那样,File.ReadAllLines() 有一个需要编码的重载,而这组字符所需的编码是 UTF-7

    工作代码

    private void btnSort_Click(object sender, EventArgs e)
            {
                //Convert to utf8
    
                string[] Accounts = File.ReadAllLines(filePath, Encoding.UTF7); // if null do something
    
                foreach (string account in Accounts)
                {
                    ListViewItem lvi = new ListViewItem(account);
                    listView1.Items.Add(lvi);
                }
    
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-23
      • 2012-03-26
      • 1970-01-01
      • 2011-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多