交换约会中的换行符

发布于 2024-10-01 07:55:56 字数 517 浏览 0 评论 0原文

我需要在预约中输入地址。地址由多个变量构成。当然我还需要一些换行符。但当我在 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;
        }

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

I need to put an adress into a appointment. The address is constructed out of several variables. Of course I also need some newlines. But "\n" doesnt result in an new line when i open the appointment in outlook.

Ok here is code snippet:

    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;
        }

Nothing special. The Problem is when i write this to the body of an appointment, then there aren't any actual newlines.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

能怎样 2024-10-08 07:55:56

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

Environment.NewLine

或者我使用 StringBuilder 类和 AppendLine() 调用添加换行符。

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

Its pretty hard to diagnose this without seeing at least an example of the string you are passing, but one thing that I tend to do in my C# code is to use the constant:

Environment.NewLine

Or I use the StringBuilder class with the AppendLine() call to add a newline.

Edit: Based on your code snippet, I would write it this way (it will be more performant as well). With your snippet, lots of strings are being allocated (because strings are immutable). The recommended approach in this case is to use 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();

Note: If you are using .Net 4.0 you can use string.IsNullOrWhitespace instead of IsNullOrEmpty to check for not just an empty string, but one that contains only whitespace.

Edit 2 - Based on your answer of needing <br /> tags instead of newlines.

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();
不再让梦枯萎 2024-10-08 07:55:56

好吧,我现在明白了。我发现约会是以html格式存储的。
所以我尝试使用 html 实体来表示 \r\n, 。但这不起作用。我终于通过使用 br 标签解决了这个问题

Ok i got it now. I found out that appointments are stored in html format.
So i tried to use the html entity for \r\n, .That didn't work. I finally solved the problem by using the br tag

东京女 2024-10-08 07:55:56

虽然您关于使用
的说法绝对正确,但换行符并不是 Exchange 在注释/约会正文中使用的唯一内容。

我最终得到了以下代码:

Regex NewlineRegex = new Regex("(\r\n)|(\r)|(\n)");

string valueToWrite = NewlineRegex.Replace(
  SecurityElement.Escape(fieldValue), "<br/>")
.Replace(" ", " ")
.Replace("'", "'"); // ' is not in HTML.

即使在那之后,您也会在正文/注释的末尾读回一个额外的 "\r\n" ,所以我必须 .TrimEnd( ) 阅读后。

While you're absolutely correct about using <br/>, newline is not the only thing Exchange eats in notes/appointment body.

I ended up with the following code:

Regex NewlineRegex = new Regex("(\r\n)|(\r)|(\n)");

string valueToWrite = NewlineRegex.Replace(
  SecurityElement.Escape(fieldValue), "<br/>")
.Replace(" ", " ")
.Replace("'", "'"); // ' is not in HTML.

And even after that you will read back an extra "\r\n" in the end of the body/notes, so I have to .TrimEnd() them after reading.

迷离° 2024-10-08 07:55:56

你应该尝试“\r\n”

you should try "\r\n"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文