在c#中替换字符串生成器中的appendLine

时间:2014-06-02 19:49:55

标签: c# .net sql-server-2005 stringbuilder

我有一个构建字符串的StringBuilder。在全部处理之后,它将保存到SQL Server表以用作csv。如何替换字符串的一部分,包括换行符?

代码是:

StringBuilder temp = new StringBuilder();

temp.AppendLine("\"Text 1 \"");
temp.AppendLine("");
temp.AppendLine("\"My Text \"");
temp.AppendLine("");
temp.AppendLine("\"Text 2 \"");
temp.AppendLine("");

我想替换中间部分但由于换行/回车而无法使其正确。

temp = temp.Replace("\"My Text \"   ");

如何获取换行符?

2 个答案:

答案 0 :(得分:0)

如果您想更换新的线路电影,请使用' \ n' (一个特殊的charcater):

  • temp = temp.Replace("\n","YOUR_REPLACING_STRING");

或使用Environment.NewLine

  • temp = temp.Replace(Environment.NewLine,"YOUR_REPLACING_STRING");

如果您想在字符串构建器中添加换行符:

  • 使用AppendLine()代替Append(),而不是插入换行符。

答案 1 :(得分:0)

最好的方法是不使用AppendLine()。如果您不想要换行,请改用Append()

但您可以用以下内容替换新行:

temp = temp.Replace(Environment.NewLine, " ");
相关问题