Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 查找ASCII字符并将其替换为新行_C#_.net_String_Replace - Fatal编程技术网

C# 查找ASCII字符并将其替换为新行

C# 查找ASCII字符并将其替换为新行,c#,.net,string,replace,C#,.net,String,Replace,我试图找到字符串中出现的每一个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 t

我试图找到字符串中出现的每一个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();
}
这不会添加新行,字符串全部保留在一行上。我不确定这里有什么问题。我也尝试过,但结果相同:

n.Replace(replaceMe, "\n");

有什么建议吗?

char.ConvertFromUtf32
虽然正确,但不是基于ASCII数值读取字符的最简单方法。(
ConvertFromUtf32
主要用于BMP之外的Unicode代码点,这会产生代理项对。这在英语或大多数现代语言中都不会遇到。)相反,您应该使用
(char)
强制转换它

当然,您可以在代码中将带有所需字符的字符串定义为文本:
“»”

然后,您的
替换将简化为:

n.Replace("»", "\n");
最后,在技术层面上,ASCII仅涵盖值在0–127范围内的字符。字符187不是ASCII码;然而,它对应于ISO 8859-1、Windows-1252和Unicode中的
»
,这三种编码目前都是最流行的编码

编辑:我刚刚测试了您的原始代码,发现它确实有效。您确定结果仍在一行上吗?调试器在单行视图中呈现字符串的方式可能存在问题:

请注意,
\r\n
序列实际上表示换行符,尽管它显示为文本。您可以通过多行显示(通过单击放大镜)进行检查:

返回一个新的
StringBuilder
,其中包含所做的更改。奇怪,我知道,但这应该行得通:

StringBuilder replaced = n.Replace(replaceMe, Environment.NewLine);

return replaced.ToString();

我们也“不确定这里有什么问题”,因为你没有告诉我们。什么问题?它和什么结果是一样的?它没有做我说它应该做的事…@banging StringBuilder的replace方法返回对实例的引用,而不是一个新实例。MyItchyChin:考虑到所有东西(除了原语)在c#中都是引用,为什么区分很重要?我暗示这是一个参考。谢谢你的回答!您的代码工作得很好,但是文本没有显示在新行上,尽管这可能是我使用的库的一个限制(文本正在使用iTextSharp写入PDF文件)。@AndrewDeForest:是的,它肯定与您使用字符串的方式有关;即使你最初的方法的结果也是正确的。iTextSharp很可能有一种引入换行符的特殊方式,类似于HTML中的

节点,因此您不能假设它会识别/尊重.NET换行符。如果您用生成PDF所用的代码片段提出问题,您可能会得到一些关于如何将字符串换行符转换为iTextSharp结构的帮助。
StringBuilder replaced = n.Replace(replaceMe, Environment.NewLine);

return replaced.ToString();