C#中的String.Replace(char,char)方法

时间:2009-03-16 18:56:49

标签: c# string

如何用空格替换\n

如果我这样做,我会得到一个空的文字错误:

string temp = mystring.Replace('\n', '');

12 个答案:

答案 0 :(得分:122)

String.Replace('\n', '')不起作用,因为''不是有效的字符文字。

如果使用String.Replace(string,string)覆盖,它应该可以工作。

string temp = mystring.Replace("\n", "");

答案 1 :(得分:16)

将“\ n”替换为“”并不能提供您想要的结果,这意味着您应该替换的内容实际上不是“\ n”,而是其他一些字符组合。

一种可能性是你应该替换的是“\ r \ n”字符组合,它是Windows系统中的换行代码。如果仅替换“\ n”(换行符)字符,它将保留“\ r”(回车)字符,这仍然可以解释为换行符,具体取决于您显示字符串的方式。

如果字符串的来源是特定于系统的,则应使用该特定字符串,否则应使用Environment.NewLine获取当前系统的换行符。

string temp = mystring.Replace("\r\n", string.Empty);

或:

string temp = mystring.Replace(Environment.NewLine, string.Empty);

答案 2 :(得分:4)

这应该有用。

string temp = mystring.Replace("\n", "");

您确定原始字符串中有实际的新行吗?

答案 3 :(得分:4)

string temp = mystring.Replace("\n", string.Empty).Replace("\r", string.Empty);

显然,这会删除'\ n'和'\ r'并且就像我知道的那样简单。

答案 4 :(得分:2)

如果您使用

string temp = mystring.Replace("\r\n", "").Replace("\n", "");

然后你不必担心你的字符串来自哪里。

答案 5 :(得分:2)

Bytes.com上找到:

string temp = mystring.Replace('\n', '\0');// '\0'代表一个空字符

答案 6 :(得分:1)

一个警告:在.NET中,换行符是“\ r \ n”。因此,如果您从文件中加载文本,则可能必须使用它而不仅仅是“\ n”

编辑>正如samuel在评论中指出的那样,“\ r \ n”不是特定于.NET的,而是特定于Windows的。

答案 7 :(得分:1)

如何创建像这样的扩展方法....

 public static string ReplaceTHAT(this string s)
 {
    return s.Replace("\n\r", "");
 }

然后当你想在任何你想要的地方替换它时,你可以做到这一点。

s.ReplaceTHAT();

最诚挚的问候!

答案 8 :(得分:1)

这是你的确切答案......

const char LineFeed = '\n'; // #10
string temp = new System.Text.RegularExpressions.Regex(
                  LineFeed
              ).Replace(mystring, string.Empty);

但是这个更好......特别是如果你想分割线(你也可以将它与Split一起使用)

const char CarriageReturn = '\r'; // #13
const char LineFeed = '\n'; // #10
string temp = new System.Text.RegularExpressions.Regex(
                  string.Format("{0}?{1}", CarriageReturn, LineFeed)
              ).Replace(mystring, string.Empty);

答案 9 :(得分:0)

string temp = mystring.Replace("\n", " ");

答案 10 :(得分:0)

@gnomixa - 你在评论中没有取得任何成果是什么意思?以下适用于VS2005。

如果您的目标是删除换行符,从而缩短字符串,请查看:

        string originalStringWithNewline = "12\n345"; // length is 6
        System.Diagnostics.Debug.Assert(originalStringWithNewline.Length == 6);
        string newStringWithoutNewline = originalStringWithNewline.Replace("\n", ""); // new length is 5
        System.Diagnostics.Debug.Assert(newStringWithoutNewline.Length == 5);

如果你的目标是用空格字符替换换行符,保持字符串长度不变,请看这个例子:

        string originalStringWithNewline = "12\n345"; // length is 6
        System.Diagnostics.Debug.Assert(originalStringWithNewline.Length == 6);
        string newStringWithoutNewline = originalStringWithNewline.Replace("\n", " "); // new length is still 6
        System.Diagnostics.Debug.Assert(newStringWithoutNewline.Length == 6);

你必须替换单字符字符串而不是字符,因为''不是要传递给Replace(string,char)的有效字符

答案 11 :(得分:0)

我知道这是一个旧帖子,但我想添加我的方法。

public static string Replace(string text, string[] toReplace, string replaceWith)
    {
        foreach (string str in toReplace)
           text = text.Replace(str, replaceWith);

        return text;
    }

使用示例:

string newText = Replace("This is an \r\n \n an example.", new string[] { "\r\n", "\n" }, "");