gpt4 book ai didi

c# - 交换约会中的换行符

转载 作者:行者123 更新时间:2023-12-03 22:56:06 26 4
gpt4 key购买 nike

我需要在约会中输入地址。地址由几个变量构成。当然,我还需要一些换行符。但是当我在 Outlook 中打开约会时,“\n”不会导致换行。

好的,这里是代码片段:

    string address = name + "\n" + strasse + "\n" + plz.ToString() + " " + ort;
if ( telefon != "") {
address = address + "\nTelefon:: " + telefon;
}
if ( natel != "") {
address = address + "\nNatel: " + natel;
}
if ( mail != "") {
address = address + "\nE-Mail: " +mail;
}

没什么特别的。问题是当我将其写入约会正文时,没有任何实际的换行符。

最佳答案

如果没有看到您传递的字符串的至少一个示例,很难对此进行诊断,但我倾向于在我的 C# 代码中做的一件事是使用常量:

Environment.NewLine

或者我使用带有 AppendLine() 调用的 StringBuilder 类来添加换行符。

编辑:根据您的代码片段,我会这样写(它也会更高效)。使用您的代码段,正在分配大量字符串(因为字符串是不可变的)。在这种情况下,推荐的方法是使用 StringBuilder。

StringBuilder address = new StringBuilder();
address.AppendLine(name);
address.AppendLine(strasse);
address.Append(plz.ToString()); // This may not be neccessary depending on the type of plz, StringBuilder has overloads that will convert base types to string for you
address.Append(" ");
address.Append(ort);
if (!string.IsNullOrEmpty(telefon))
{
address.AppendLine();
address.Append("Telefon:: ");
address.Append(telefon);
}
if (!string.IsNullOfEmpty(natel))
{
address.AppendLine();
address.Append("Natel: ");
address.Append(natel);
}
if (!string.IsNullOrEmpty(mail))
{
address.AppendLine();
address.Append("E-Mail: ");
address.Append(mail);
}

return address.ToString();

注意:如果您使用的是 .Net 4.0,则可以使用 string.IsNullOrWhitespace 而不是 IsNullOrEmpty 来检查不仅是空字符串,而且是只包含空格的字符串。

编辑 2 - 根据您需要
标签而不是换行符的回答。

const string newLine = " <br /> ";
StringBuilder address = new StringBuilder();
address.Append(name);
address.Append(newLine);
address.Append(strasse);
address.Append(newLine);
address.Append(plz.ToString()); // This may not be neccessary depending on the type of plz, StringBuilder has overloads that will convert base types to string for you
address.Append(" ");
address.Append(ort);
if (!string.IsNullOrEmpty(telefon))
{
address.Append(newLine);
address.Append("Telefon:: ");
address.Append(telefon);
}
if (!string.IsNullOfEmpty(natel))
{
address.Append(newLine);
address.Append("Natel: ");
address.Append(natel);
}
if (!string.IsNullOrEmpty(mail))
{
address.Append(newLine);
address.Append("E-Mail: ");
address.Append(mail);
}

return address.ToString();

关于c# - 交换约会中的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4124348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com