.NET:如何确定编码是否支持字符串中的所有字符? [英] .NET: How do I tell if an encoding supports all the chars in my string?

查看:81
本文介绍了.NET:如何确定编码是否支持字符串中的所有字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要输出很多文本,其中包括来自多种语言的各种字符。有时候,我需要以Unicode以外的其他字符编码输出文本(例如Shift-JIS或ISO-8859-2),以匹配要转到的页面。

I've got lots of text that I need to output, which includes all sorts of characters from many languages. Sometimes I need to output the text in character encodings other than Unicode (eg, Shift-JIS, or ISO-8859-2), in order to match the page it's going to.

如果文本包含编码无法处理的字符(例如,ISO-8859-2编码输出中的日文字符),我将在输出中以奇数字符结尾。我可以逃脱它们,但只有在确实必要时,我才愿意这样做。

If the text has characters that the encoding can't handle (eg, Japanese characters in ISO-8859-2 encoded output) I end up with odd characters in the output. I can escape them, but I'd rather do that only if it's really necessary.

所以,我的问题是:有一种方法可以提前告知编码是否可以处理字符串中的所有字符?

So, my question is this: Is there a way I can tell ahead of time if an encoding can handle all the characters in my string?

编辑:
我认为EncoderFallback可能是我提出的问题的正确答案。不幸的是,在我的特定情况下它似乎不起作用。我的想法是将字符转换为它们的HTML实体等效项(例如モ而不是モ)。但是,编码器仅转换找到的第一个此类字符,如果我设置Response.ContentEncoding,则它根本不会调用我的EncoderFallback。

I think the EncoderFallback is probably the right answer to the question I asked. Unfortunately it doesn't seem to work in my particular situation. My thought was to convert the characters to their HTML entity equivalents (eg, モ instead of モ). However, the encoder only converts the first such character it finds, and if I set the Response.ContentEncoding it never calls my EncoderFallback at all.

推荐答案

您可以编写自己的EncoderFallback类,然后在编码之前将其分配给编码器。

You can write your own EncoderFallback class assign that to the encoder before encoding.

使用这种方法,您无需进行任何高级操作(这很可能只是处理输出字符串以查找问题)。

Using this approach you need do nothing in advanced (which likely would be simply processing the output string looking for problems).

您的Fallback类仅需要处理编码中没有字符值的替换。

Instead your Fallback class need only handle replacements where the encoding does not have a value for a character.

这篇关于.NET:如何确定编码是否支持字符串中的所有字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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