如何更换在C#中的字符串文字? [英] How to replace text in string in C#?

查看:133
本文介绍了如何更换在C#中的字符串文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码特定部分,但它不工作。

I have this particular piece of code, but its not working.

text = text.Replace("\xEF\xBF\xBD", "?");



还有没有人知道如何替换文本\xEF\ xBF\xBD在C#字符串

推荐答案

简答题(猜测了一下):

text = text.Replace("\xFFFD", "?");

和了解的 Unicode的字符编码的,尤其是的 UTF-8

And learn about Unicode and character encodings, especially utf-8.

龙答:

好了,你的意思是\xEF\xBF\xBD的字面?也就是说,由这些字符的字符串:

Well, do you mean "\xEF\xBF\xBD" literally? That is, a string consisting of these characters:

backslash, uppercase latin character E, uppercase latin character F, backslash, uppercase latin character B, uppercase latin character F, backslash, uppercase latin character B, uppercase latin character D

然后,答案应该是:

text = text.Replace(@"\xEF\xBF\xBD", "?");



还是你这是由C#转义序列\xEF\xBF描述的字符序列\xBD,即:

Or do you sequences of characters which are described by the C# escape sequence "\xEF\xBF\xBD", namely:

LATIN SMALL LETTER I WITH DIAERESIS, INVERTED QUESTION MARK, VULGAR FRACTION ONE HALF

(这将显示为�)然后,你的代码是正确的:

(which would be displayed as "�)? Then, the your code would be correct:

text = text.Replace("\xEF\xBF\xBD", "?");

或你想要替换的字节

EF BF BD

(这实际上可能是UTF-8 Unicode替换字符,FFFD,这往往显示为)的代表性?

(which could actually be the utf-8 representation of the unicode replacement character, FFFD, which is often displayed as"�")?

这只是胡乱猜测,而是凭直觉说你实际上。要实现后者现在,一个.net字符串中包含的字符,而不是字节,但假设你已经从一个文件为UTF-8,答案应该是阅读这些字节例如:

This is just a wild guess, but by intuition says you actually want to achieve the latter. Now, a .Net string contains characters, not bytes, but assuming that you have read these bytes e.g. from a file as utf-8, the answer would be:

text = text.Replace("\xFFFD", "?");

这篇关于如何更换在C#中的字符串文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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