C# 查找并用新行替换 ASCII 字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10743571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find and replace ASCII character with a new line
提问by Andrew De Forest
I am trying to find every occurrence of an ASCII character in a string and replace it with a new line. Here is what I have so far:
我正在尝试查找字符串中出现的每个 ASCII 字符并将其替换为新行。这是我到目前为止所拥有的:
public string parseText(string inTxt)
{
//String builder based on the string passed into the method
StringBuilder n = new StringBuilder(inTxt);
//Convert the ASCII character we're looking for to a string
string replaceMe = char.ConvertFromUtf32(187);
//Replace all occurences of string with a new line
n.Replace(replaceMe, Environment.NewLine);
//Convert our StringBuilder to a string and output it
return n.ToString();
}
This does not add in a new line and the string all remains on one line. I'm not sure what the problem is here. I have tried this as well, but same result:
这不会添加新行,并且字符串都保留在一行上。我不确定这里有什么问题。我也试过这个,但结果相同:
n.Replace(replaceMe, "\n");
Any suggestions?
有什么建议?
采纳答案by Douglas
char.ConvertFromUtf32, whilst correct, is not the simplest way to read a character based on its ASCII numeric value. (ConvertFromUtf32is mainly intended for Unicode code points that lie outside the BMP, which result in surrogate pairs. This is not something you'd encounter in English or most modern languages.) Rather, you should just cast it using (char).
char.ConvertFromUtf32,虽然正确,但不是根据其 ASCII 数值读取字符的最简单方法。(ConvertFromUtf32主要用于位于 BMP 之外的 Unicode 代码点,这会导致代理对。这不是您在英语或大多数现代语言中会遇到的。)相反,您应该使用(char).
char c = (char)187;
string replaceMe = c.ToString();
You may, of course, define a string with the required character as a literal in your code: "?".
当然,您可以在代码中定义一个带有所需字符的字符串作为文字:"?".
Your Replacewould then be simplified to:
Replace然后您将被简化为:
n.Replace("?", "\n");
Finally, on a technical level, ASCII only covers characters whose value lies in the 0–127 range. Character 187 is not ASCII; however, it corresponds to ?in ISO 8859-1, Windows-1252, andUnicode, which collectively are by far the most popular encodings in use today.
最后,在技术层面上,ASCII 仅涵盖值在 0-127 范围内的字符。字符 187 不是 ASCII;但是,它对应?于 ISO 8859-1、Windows-1252和Unicode,它们合起来是当今最流行的编码。
Edit: I just tested your original code, and found that it actually worked. Are you sure the result remains on one line? It might be an issue with the way the debugger renders strings in single-line view:
编辑:我刚刚测试了您的原始代码,发现它确实有效。您确定结果保留在一行上吗?调试器在单行视图中呈现字符串的方式可能存在问题:


Note that the \r\nsequences actually dorepresent newlines, despite being displayed as literals. You can check this from the multi-line display (by clicking on the magnifying glass):
请注意,尽管显示为文字,但\r\n序列实际上确实表示换行符。您可以从多行显示(通过单击放大镜)进行检查:


回答by Harry Cutts
StringBuilder.Replacereturns a new StringBuilderwith the changes made. Strange, I know, but this should work:
StringBuilder.Replace返回StringBuilder带有所做更改的新对象。奇怪,我知道,但这应该有效:
StringBuilder replaced = n.Replace(replaceMe, Environment.NewLine);
return replaced.ToString();

