0Day Forums
How to insert newline in string literal? - Printable Version

+- 0Day Forums (https://zeroday.vip)
+-- Forum: Coding (https://zeroday.vip/Forum-Coding)
+--- Forum: C# (https://zeroday.vip/Forum-C)
+--- Thread: How to insert newline in string literal? (/Thread-How-to-insert-newline-in-string-literal)



How to insert newline in string literal? - lyrist681 - 07-24-2023

In .NET I can provide both `\r` or `\n` string literals, but there is a way to insert
something like "new line" special character like `Environment.NewLine` static property?






RE: How to insert newline in string literal? - ethmyphitis315197 - 07-24-2023

var sb = new StringBuilder();
sb.Append(first);
sb.AppendLine(); // which is equal to Append(Environment.NewLine);
sb.Append(second);
return sb.ToString();


RE: How to insert newline in string literal? - blisterwort438000 - 07-24-2023



static class MyClass
{
public const string NewLine="\n";
}

string x = "first line" + MyClass.NewLine + "second line"


RE: How to insert newline in string literal? - mountablyyg - 07-24-2023

If you want a const string that contains Environment.NewLine in it you can do something like this:

const string stringWithNewLine =
@"first line
second line
third line";

EDIT
----------
Since this is in a const string it is done in compile time therefore it is the compiler's interpretation of a newline. I can't seem to find a reference explaining this behavior but, I can prove it works as intended. I compiled this code on both Windows and Ubuntu (with Mono) then disassembled and these are the results:

![Disassemble on Windows][1]
![Disassemble on Ubuntu][2]

[1]:


[2]:


As you can see, in Windows newlines are interpreted as \r\n and on Ubuntu as \n


RE: How to insert newline in string literal? - corwun796 - 07-24-2023

One more way of convenient placement of Environment.NewLine in format string.
The idea is to create string extension method that formats string as usual but also replaces {nl} in text with Environment.NewLine


***Usage***

" X={0} {nl} Y={1}{nl} X+Y={2}".FormatIt(1, 2, 1+2);
gives:
X=1
Y=2
X+Y=3





***Code***

///<summary>
/// Use "string".FormatIt(...) instead of string.Format("string, ...)
/// Use {nl} in text to insert Environment.NewLine
///</summary>
///<exception cref="ArgumentNullException">If format is null</exception>
[StringFormatMethod("format")]
public static string FormatIt(this string format, params object[] args)
{
if (format == null) throw new ArgumentNullException("format");

return string.Format(format.Replace("{nl}", Environment.NewLine), args);
}

***Note***

1. If you want ReSharper to highlight your parameters, add attribute to the method above

[StringFormatMethod("format")]

2. This implementation is obviously less efficient than just String.Format

3. Maybe one, who interested in this question would be interested in the next question too:

[To see links please register here]




RE: How to insert newline in string literal? - adelezz - 07-24-2023

string myText =
@"<div class=""firstLine""></div>
<div class=""secondLine""></div>
<div class=""thirdLine""></div>";

> that's not it:

string myText =
@"<div class=\"firstLine\"></div>
<div class=\"secondLine\"></div>
<div class=\"thirdLine\"></div>";