StringBuilder.Append 在长字符串 C# 的情况下将文本放在新行中
StringBuilder.Append puts text in the new line in case of long string C#
正如你在下面看到的,我想在同一行中附加一些文本,但是当一部分太长时,它位于新行中,结果变成两行字符串。
using (var wr = new StreamWriter(filepath,true))
{
var strBuilder = new StringBuilder();
strBuilder.Append(UtilityBillId);
strBuilder.Append("|B|");
strBuilder.Append(billId);
strBuilder.Append("|B|");
strBuilder.Append(paymentId);
strBuilder.Append("|B|");
strBuilder.Append(stepTitle);
strBuilder.Append("|B|");
strBuilder.Append(resultCode);
strBuilder.Append("|B|");
strBuilder.Append(resultMessage);
strBuilder.Append("|B|");
strBuilder.Append(Common._getCurrentDateTime());
wr.WriteLine(strBuilder.ToString());
}
当 resultMessage
很长时,它的一部分位于新行中
值得一提的是,当我使用 File.ReadAllLines(path)
时,它在文本文件中显示了相同的结果。
您可以通过下面的link查看结果文本
此代码将生成只有一行的文件:
Regex r = new Regex("[\r\n]");
var strBuilder = new StringBuilder();
strBuilder.Append(UtilityBillId);
strBuilder.Append("|B|");
strBuilder.Append(billId);
strBuilder.Append("|B|");
strBuilder.Append(paymentId);
strBuilder.Append("|B|");
strBuilder.Append(r.Replace(stepTitle, ""));
strBuilder.Append("|B|");
strBuilder.Append(resultCode);
strBuilder.Append("|B|");
strBuilder.Append(r.Replace(resultMessage, ""));
strBuilder.Append("|B|");
strBuilder.AppendLine(Common._getCurrentDateTime());
File.AppendAllText(filepath, strBuilder.ToString());
我假设任何以 ID 结尾的都是 int,而且我还假设 datetime 中没有换行符
测试它,看看它是否也 "produces a file with two lines" - 如果是,我 100% 确定这是您的编辑器换行。
字符串生成器与新行无关,除非有 \n
或者您手动附加一个 System.getProperty("line.separator")
。唯一随字符串长度变化的是内存分配。
这里有一部分文档解释它:
StringBuilder.Length属性表示StringBuilder对象当前包含的字符数。如果向 StringBuilder 对象添加字符,它的长度会增加,直到它等于 StringBuilder.Capacity 属性 的大小,它定义了对象可以包含的字符数。如果增加的字符数导致StringBuilder对象的长度超过了当前的容量,则分配新的内存,将Capacity属性的值翻倍,向StringBuilder对象中添加新的字符,其Length属性 进行调整。 StringBuilder 对象的额外内存是动态分配的,直到它达到 StringBuilder.MaxCapacity 属性 定义的值。当达到最大容量时,无法为 StringBuilder 对象分配更多内存,尝试添加字符或将其扩展到超过其最大容量时会引发 ArgumentOutOfRangeException 或 OutOfMemoryException 异常。
有关详细信息,您可以查看官方 Microsoft 线程:
https://docs.microsoft.com/en-us/dotnet/api/system.text.stringbuilder?view=netframework-4.8
如果您的任何字符串包含 \n
,您的问题是您用于可视化文本的工具将其换行。
根据 'Caius Jard' 的回答,我更改了我的代码并解决了我的问题。
Regex r = new Regex("[\r\n]");
var strBuilder = new StringBuilder();
strBuilder.Append(UtilityBillId);
strBuilder.Append("|B|");
strBuilder.Append(billId);
strBuilder.Append("|B|");
strBuilder.Append(paymentId);
strBuilder.Append("|B|");
strBuilder.Append(r.Replace(stepTitle, ""));
strBuilder.Append("|B|");
strBuilder.Append(resultCode);
strBuilder.Append("|B|");
strBuilder.Append(r.Replace(resultMessage, ""));
strBuilder.Append("|B|");
strBuilder.AppendLine(Common._getCurrentDateTime());
wr.WriteLine(strBuilder.ToString());
正如你在下面看到的,我想在同一行中附加一些文本,但是当一部分太长时,它位于新行中,结果变成两行字符串。
using (var wr = new StreamWriter(filepath,true))
{
var strBuilder = new StringBuilder();
strBuilder.Append(UtilityBillId);
strBuilder.Append("|B|");
strBuilder.Append(billId);
strBuilder.Append("|B|");
strBuilder.Append(paymentId);
strBuilder.Append("|B|");
strBuilder.Append(stepTitle);
strBuilder.Append("|B|");
strBuilder.Append(resultCode);
strBuilder.Append("|B|");
strBuilder.Append(resultMessage);
strBuilder.Append("|B|");
strBuilder.Append(Common._getCurrentDateTime());
wr.WriteLine(strBuilder.ToString());
}
当 resultMessage
很长时,它的一部分位于新行中
值得一提的是,当我使用 File.ReadAllLines(path)
时,它在文本文件中显示了相同的结果。
您可以通过下面的link查看结果文本
此代码将生成只有一行的文件:
Regex r = new Regex("[\r\n]");
var strBuilder = new StringBuilder();
strBuilder.Append(UtilityBillId);
strBuilder.Append("|B|");
strBuilder.Append(billId);
strBuilder.Append("|B|");
strBuilder.Append(paymentId);
strBuilder.Append("|B|");
strBuilder.Append(r.Replace(stepTitle, ""));
strBuilder.Append("|B|");
strBuilder.Append(resultCode);
strBuilder.Append("|B|");
strBuilder.Append(r.Replace(resultMessage, ""));
strBuilder.Append("|B|");
strBuilder.AppendLine(Common._getCurrentDateTime());
File.AppendAllText(filepath, strBuilder.ToString());
我假设任何以 ID 结尾的都是 int,而且我还假设 datetime 中没有换行符
测试它,看看它是否也 "produces a file with two lines" - 如果是,我 100% 确定这是您的编辑器换行。
字符串生成器与新行无关,除非有 \n
或者您手动附加一个 System.getProperty("line.separator")
。唯一随字符串长度变化的是内存分配。
这里有一部分文档解释它:
StringBuilder.Length属性表示StringBuilder对象当前包含的字符数。如果向 StringBuilder 对象添加字符,它的长度会增加,直到它等于 StringBuilder.Capacity 属性 的大小,它定义了对象可以包含的字符数。如果增加的字符数导致StringBuilder对象的长度超过了当前的容量,则分配新的内存,将Capacity属性的值翻倍,向StringBuilder对象中添加新的字符,其Length属性 进行调整。 StringBuilder 对象的额外内存是动态分配的,直到它达到 StringBuilder.MaxCapacity 属性 定义的值。当达到最大容量时,无法为 StringBuilder 对象分配更多内存,尝试添加字符或将其扩展到超过其最大容量时会引发 ArgumentOutOfRangeException 或 OutOfMemoryException 异常。
有关详细信息,您可以查看官方 Microsoft 线程: https://docs.microsoft.com/en-us/dotnet/api/system.text.stringbuilder?view=netframework-4.8
如果您的任何字符串包含 \n
,您的问题是您用于可视化文本的工具将其换行。
根据 'Caius Jard' 的回答,我更改了我的代码并解决了我的问题。
Regex r = new Regex("[\r\n]");
var strBuilder = new StringBuilder();
strBuilder.Append(UtilityBillId);
strBuilder.Append("|B|");
strBuilder.Append(billId);
strBuilder.Append("|B|");
strBuilder.Append(paymentId);
strBuilder.Append("|B|");
strBuilder.Append(r.Replace(stepTitle, ""));
strBuilder.Append("|B|");
strBuilder.Append(resultCode);
strBuilder.Append("|B|");
strBuilder.Append(r.Replace(resultMessage, ""));
strBuilder.Append("|B|");
strBuilder.AppendLine(Common._getCurrentDateTime());
wr.WriteLine(strBuilder.ToString());