修正了字符串中的特殊字符 [英] Fix Special Characters in String

查看:122
本文介绍了修正了字符串中的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,简单地说从SQL数据库中读取值,并将其写入到一个制表符分隔文本文件。

问题是,一些在数据库中的值有特殊字符(TM,破折号,省略号等),当写入文本文件,格式丢失,他们遇到的垃圾A或¢â €等

当值在立即窗口中查看,之前它被写入TXT文件,一切都看起来很好。我的猜测是,这是编码的问题。不过,我并不真正知道如何着手,去哪里找,还是要寻找什么。

这是ASCII或UTF-8?如果这是其中的一个我怎么纠正之前的书面文本文件。

下面就是我如何建立文本文件(其中feedStr是一个StringBuilder)

  objReader =新的StreamWriter(文件路径)
objReader.Write(feedStr)
objReader.Close()


解决方案

有关的StreamWriter默认的编码是UTF8(无字节顺序标记)。你的结果文件是确定的,问题是你怎么在事后打开呢?如果你在一个UTF8能够文本编辑器打开它,人物看起来应该你想要的方式。

您也可以写在另一种编码的文本文件,例如ISO-8859-1(latin1的)

  objReader =新的StreamWriter(文件路径,假Encoding.GetEncoding(ISO-8859-1))

I've got a program that in a nutshell reads values from a SQL database and writes them to a tab-delimited text file.

The issue is that some of the values in the database have special characters (TM, dash, ellipsis, etc.) When written to the text file, the formatting is lost and they come across as junk "â„¢ or â€" etc"

When the value is viewed in the immediate window, before it is written to the txt file, everything looks fine. My guess is that this is an issue of encoding. But, I'm not real sure how to proceed, where to look, or what to look for.

Is this ASCII or UTF-8? If it's one of those how do I correct it before it's written to the text file.

Here's how I build the text file (where feedStr is a StringBuilder)

objReader = New StreamWriter(filePath)
objReader.Write(feedStr)
objReader.Close()

解决方案

The default encoding for StreamWriter is UTF8 (with no byte order mark). Your result file is ok, the question is what do you open it in afterwards? If you open it in a UTF8 capable text editor, the characters should look the way you want.

You can also write the text file in another encoding, for example iso-8859-1 (latin1)

objReader = New StreamWriter(filePath, false, Encoding.GetEncoding("iso-8859-1"))

这篇关于修正了字符串中的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆