Java 8 multiline string example
Java offers many different ways to work with multiline strings: by streams, String.join() etc. This post try to show all ways and to show different ways of working with multiline strings in Java.
In this post:
- Java 8 multi-line string by stream and joining
- Java 8 multiline string - String.join()
- prior Java 8 multiline strings - StringBuilder().append()
- multi-line with StringBuilder and collections
- IntelliJ/Eclipse multiline string - "string1" + "string2"
- prior Java 8 multi-line with String.format
- Split multi-line string
- Timings and performance prefer streams/joining and not StringBuilder
Java 8 multi-line string by stream and joining
The first way is by using streams and joining. This one is elegant and more natural if you have all data in a collection:
List<String> words = new ArrayList<String>();
words.add("Java 7");
words.add("Java 8");
words.add("Java 9");
String summary = words.stream().collect( Collectors.joining("\n" ) );
System.out.println(summary);
result:
Java 7
Java 8
Java 9
Java 8 multi-line string by join
The second way to do Multiline string in Java 8 is by method join. The example below takes the OS separator and combines all lines in one multiline String. This one is more appropriate when you have small known number of lines:
String sampleString = String.join(
System.getProperty("line.separator"),
"First line",
"Second line",
"Third line",
"Forth line"
);
System.out.println(sampleString);
result:
First line
Second line
Third line
Forth line
prior Java 8 multi-line with StringBuilder
For older versions of Java (prior 8) you can use string builder and append method :
String s = new StringBuilder()
.append("line1\n")
.append("line2\n")
.append("line3\n")
.toString();
System.out.println(s);
result:
line1
line2
line3
multi-line with StringBuilder and collections
If you have your data in a collection - let say list of strings and you want to make it to mulitline java string than you can use this example:
List<String> words = new ArrayList<String>();
words.add("Java 7");
words.add("Java 8");
words.add("Java 9");
String s3 = "";
for (String word : words ) {
s3 = new StringBuilder().append(s3 + word + "\n").toString();
}
System.out.println(s3);
result:
Java 7
Java 8
Java 9
prior Java 8 multi-line with String.format
Another possible way for small number of lines is by String.format:
String s2 = String.format("%s%s%s"
, "line1\n"
, "line2\n"
, "line3\n"
);
System.out.println(s2);
result:
line1
line2
line3
Intellij IDE / Eclipse multiline string
If you use Intellij IDE or Eclipse then you can simply start writing your text with new lines or even paste multiline text and its going to be converted to:
-
Before
String[] items = sampleString.split(separator);
List itemList = Arrays.asList(items);
System.out.println(itemList); -
After
"String[] items = sampleString.split(separator);\n" + " List<String> itemList = Arrays.asList(items);\n" + " System.out.println(itemList);"
Split multi-line string
If you want to iterate a multiline string then you can do it with the String method split:
import java.util.Arrays;
import java.util.List;
public class StringTest {
public static void main(java.lang.String[] args) {
String separator = System.getProperty("line.separator");
String sampleString = String.join(
separator,
"First line",
"Second line",
"Third line",
"Forth line"
);
String[] items = sampleString.split(separator);
List<String> itemList = Arrays.asList(items);
System.out.println(itemList);
}
}
the result is:
[First line, Second line, Third line, Forth line]
Timings and performance
The most efficient and fast way is by using: stream and joining. Below you can find some of the resutls from the tests: Numbers of
10 000 times:
StringBuilder and collection - 1.333837 secs
100 000 times:
StringBuilder and collection -35.342295 secs
Stream and joining - 0.286829 secs
So the Stream and joining is much more faster in comparisson to StringBuilder if the number of lines is growing rapidly.
Test code:
long start = System.nanoTime();
//
// Test code start
//
List<String> words = new ArrayList<String>();
for (int i = 0; i < 1000; i++){
words.add(("" + i));
}
String summary = words.stream().collect( Collectors.joining("\n" ) );
System.out.println(summary);
//
// Test code end
//
long elapsedTime = System.nanoTime() - start;
double secondDecimalPrecision = (double)elapsedTime / 1000000000.0;