Is There a Newline Constant Defined in Java Like Environment.Newline in C#

Is there a Newline constant defined in Java like Environment.Newline in C#?

As of Java 7 (and Android API level 19):

System.lineSeparator()

Documentation: Java Platform SE 7


For older versions of Java, use:

System.getProperty("line.separator");

See https://java.sun.com/docs/books/tutorial/essential/environment/sysprop.html for other properties.

String constant for a new line + tab?

Provided you declare the string as const, as above, there is absolutely no difference in terms of efficiency. Any constant will be substituted at compile time and use an interned string.

Unfortunately, the second option is not a compile time constant, and will not compile. In order to use it, you'd need to declare it as:

internal static readonly string segment = Environment.NewLine + "\t"; 

I, personally, find this very clear in terms of intent, and it would be my preference, even though it's not going to be a compile time constant. The extra overhead/loss of efficiency is so incredibly minor that I would personally choose the clear intent and legible code over the compile time constant.

Note that using Environment.NewLine also has the benefit of being correct if you port this code to Mono, and your goal is to use the current platforms line separator. The first will be incorrect on non-Windows platforms in that specific case. If your goal is to specifically include "\r\n\t", and do not desire the platform-specific line separator, then Environment.NewLine would be an inappropriate choice.

How to insert newline in string literal?

Well, simple options are:

  • string.Format:

    string x = string.Format("first line{0}second line", Environment.NewLine);
  • String concatenation:

    string x = "first line" + Environment.NewLine + "second line";
  • String interpolation (in C#6 and above):

    string x = $"first line{Environment.NewLine}second line";

You could also use \n everywhere, and replace:

string x = "first line\nsecond line\nthird line".Replace("\n",
Environment.NewLine);

Note that you can't make this a string constant, because the value of Environment.NewLine will only be available at execution time.

Newline character in StringBuilder

I would make use of the Environment.NewLine property.

Something like:

StringBuilder sb = new StringBuilder();
sb.AppendFormat("Foo{0}Bar", Environment.NewLine);
string s = sb.ToString();

Or

StringBuilder sb = new StringBuilder();
sb.Append("Foo");
sb.Append("Foo2");
sb.Append(Environment.NewLine);
sb.Append("Bar");
string s = sb.ToString();

If you wish to have a new line after each append, you can have a look at Ben Voigt's answer.

Adding a newline into a string in C#

Use Environment.NewLine whenever you want in any string. An example:

string text = "fkdfdsfdflkdkfk@dfsdfjk72388389@kdkfkdfkkl@jkdjkfjd@jjjk@";

text = text.Replace("@", "@" + System.Environment.NewLine);

Easiest way to split a string on newlines in .NET?

To split on a string you need to use the overload that takes an array of strings:

string[] lines = theText.Split(
new string[] { Environment.NewLine },
StringSplitOptions.None
);

Edit:

If you want to handle different types of line breaks in a text, you can use the ability to match more than one string. This will correctly split on either type of line break, and preserve empty lines and spacing in the text:

string[] lines = theText.Split(
new string[] { "\r\n", "\r", "\n" },
StringSplitOptions.None
);

Replace Line Breaks in a String C#

Use replace with Environment.NewLine

myString = myString.Replace(System.Environment.NewLine, "replacement text"); //add a line terminating ;

As mentioned in other posts, if the string comes from another environment (OS) then you'd need to replace that particular environments implementation of new line control characters.

How to append a newline to StringBuilder

It should be

r.append("\n");

But I recommend you to do as below,

r.append(System.getProperty("line.separator"));

System.getProperty("line.separator") gives you system-dependent newline in java. Also from Java 7 there's a method that returns the value directly: System.lineSeparator()



Related Topics



Leave a reply



Submit