简体   繁体   中英

Constant for HTML line breaks in C#?

I have a function ParametersToHtml() . It converts parameters to an HTML string suitable for display on a page. It uses <br /> in a bunch of places. Here is what that looks like:

private string ParametersToHtml() {
    var result = "";

    if (Parameters.Count > 0)
        result = Parameters.Name + ": " + Parameters.Value;

    foreach (var param in Parameters.Skip(1))
        result += "<br />" + param.Name + ": " + param.Value

    return result;
}

The "<br />" construct isn't terribly ugly, but it isn't pretty either. I know that C# has Environment.Newline for regular newlines. Is there a constant for <br /> ?

(Yes, I forgot to use a StringBuilder in my example foreach . Whoops.)

I couldn't find a constant like that, but here is the next best thing!

public static class HtmlConst
{
    public const string Br = @"<br />";
}

Then just call it:

foreach (var param in Parameters.Skip(1))
        result += HtmlConst.Br + param.Name + ": " + param.Value

why dont you add "@" at the begining of the string

foreach (var param in Parameters.Skip(1))
    result += @"<br />" + param.Name + ": " + param.Value

no need to "\\"<br />\\""

you can read more in this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM