Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 489 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to insert newline in string literal?

#1
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?



Reply

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

#3


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

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

#4
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
Reply

#5
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]

Reply

#6
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>";
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through