Java stringbuilder append new line

Class StringBuilder

A mutable sequence of characters. This class provides an API compatible with StringBuffer , but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.

The principal operations on a StringBuilder are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string builder. The append method always adds these characters at the end of the builder; the insert method adds the characters at a specified point.

For example, if z refers to a string builder object whose current contents are » start «, then the method call z.append(«le») would cause the string builder to contain » startle «, whereas z.insert(4, «le») would alter the string builder to contain » starlet «.

In general, if sb refers to an instance of a StringBuilder , then sb.append(x) has the same effect as sb.insert(sb.length(), x) .

Every string builder has a capacity. As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer overflows, it is automatically made larger.

Читайте также:  Html php содержимое элемента

Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended that StringBuffer be used.

Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.

Источник

Java StringBuilder Append New Line Example (StringBuffer)

Java StringBuilder append new line example shows how to append new line in StringBuilder or StringBuffer object. It also shows how to add a new line to StringBuilder/StringBuffer using various methods.

How to append a new line in StringBuilder or StringBuffer in Java?

Many times we use StringBuilder or StringBuffer to create and store multiline content or text. In such cases, we want to append or add a new line to the StringBuilder content.

The StringBuilder or StringBuffer class does not provide any direct method to append a new line to the content. However, you can utilize the overloaded String version of the append method of StringBuilder/StringBuffer class to append a new line to it.

The simple version of the code will look like below.

As you can see from the output when we print the content of the StringBuilder object we can see two lines in the output. The same code works for the StringBuffer as well because they both have similar APIs.

What is the better way to append a new line?

The above code is fairly simple and works. However, a better approach is to use system property instead of using \n. The new line is represented differently in different operating systems. For Windows, new line is \r (carriage return) followed by \n i.e. \r\n. However, in the Unix system, it is just \n.

Because of this difference at the operating system level, the above code might not work in any particular OS. For this reason, the Java System class provides getProperty method to retrieve system specific line separator. The “line.separator” property returns the system-specific line separator.

Источник

Class StringBuilder

A mutable sequence of characters. This class provides an API compatible with StringBuffer , but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.

The principal operations on a StringBuilder are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string builder. The append method always adds these characters at the end of the builder; the insert method adds the characters at a specified point.

For example, if z refers to a string builder object whose current contents are » start «, then the method call z.append(«le») would cause the string builder to contain » startle «, whereas z.insert(4, «le») would alter the string builder to contain » starlet «.

In general, if sb refers to an instance of a StringBuilder , then sb.append(x) has the same effect as sb.insert(sb.length(), x) .

Every string builder has a capacity. As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer overflows, it is automatically made larger.

Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended that StringBuffer be used.

Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.

Источник

Оцените статью