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
Printing My MAC's Serial Number in Java Using Unix Commands
How Does Java's Preparedstatement Work
Java Regular Expressions and Dollar Sign
Java Jdbc Access Denied for User
Simple Way to Count Character Occurrences in a String
How to Configure JPA for Testing in Maven
Convert Seconds Value to Hours Minutes Seconds
JSONmanagedreference VS JSONbackreference
Can Constructor Return a Null Object
Please Explain About Insertable=False and Updatable=False in Reference to the JPA @Column Annotation
How to Change Timezone for a Java.Util.Calendar/Date
Java Lib or App to Convert CSV to Xml File
Uses for the Java Void Reference Type
Highlights Substring in the Tablecell(S) Which Is Using for Jtable Filetering
Directly Convert CSV File to JSON File Using the Jackson Library