Split on new line java

How to split a string by new line in Java

Newline is a control character in character encoding specification that is used to signify the end of a line of text and the start of a new one. Every operating system has different newline. For example, UNIX and Mac OS have \r whereas Windows has \r\n .

In Java, you need to use \\r?\\n as a regular expression to split a string into an array by new line. Here is an example:

// create a new string String text = "Java" + "\r\n" + "is a" + "\n" + "server-side" + "\r\n" + "programming" + "\n" + "language."; // split by new line String[] tokens = text.split("\\r?\\n"); // print values for (String t : tokens)  System.out.println(t); > 

The above code will produce the following output:

Java is a server-side programming language. 

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like.

Источник

Java split String by new line Example

Java split String by new line example shows how to split string by new line in Java using regular expression as well as ignore empty lines. Example also covers files created in Windows, Unix and Mac OS.

How to split String by a new line in Java?

While processing a file or processing text area inputs you need to split string by new line characters to get each line. You can do this by using regular expressions in Java.

Different operating systems use different characters to represent a new line as given below.

While splitting a string by a new line, we need to take care of all the possible new line characters given above. So basically, we need to look for zero or one \r followed by \n or just \r. Converting it to regex pattern will become “\\r?\\n|\\r” where,

The above pattern will cover all three OS newline characters.

How to include empty trailing lines?

The above pattern ignores the empty trailing line at the end of the string or file.

The last empty line is not included in the output. You can include the trailing empty lines by using a limit parameter in the split method as given below.

How to ignore empty lines in between?

Sometimes string or file contains empty lines between the content as given below.

There are two empty lines between Line1 and Line 2. If you want to ignore empty line between lines, you can use the «[\r?\n|\r]+» regex pattern where,

Our pattern will match one or more newlines since we applied “+” to the character group.

Note: We do not need to escape \r or \n because they are inside the character group ([]).

How to split string by new line in Java 8?

If you are using Java 8, you can use «\R» pattern instead of «\\r?\\n|\\r» pattern. The «\R» pattern covers all the new line characters.

How to split string by new line using line.separator? (Not recommended)

The line.separator is a system property that gives default newline characters for the operating system. It can be used to split the string as given below.

Why it is not recommended? In the above example, the string contains \r\n as a new line and the output was generated in the Windows machine. So, when we get “line.separator” property, it returned Windows new line which is \r\n and program worked as expected.

If the same code was ran on Unix, “line.separator” would have returned \n and our code would have failed. This approach is not recommended for the same reason, it is platform dependent. Means you will not be able to process file generated in a different environment.

You can also split string by dot, pipe, and comma or you can split string in equal lengths as well.

Please let me know your views in the comments section below.

About the author

I have a master’s degree in computer science and over 18 years of experience designing and developing Java applications. I have worked with many fortune 500 companies as an eCommerce Architect. Follow me on LinkedIn and Facebook.

Источник

Java Split String by Space and Newline — Java Tutorial for Beginners

Java Split String by Space and Newline

There are several ways to read a plain text file in Java, such as BufferedReader, java.nio.file.Files.lines() . In this example, we can use java.nio.file.Files.readAllBytes() method to read file content into string. The text file is stored at C:/data/fruits.txt with following contents.

Cherry Banana Chico fruit Jackfruit Kiwifruit Orange Pineapple Mango Apple
String filePath = "c:/data/fruits.txt"; String contents = null; try < contents = new String(Files.readAllBytes(Paths.get(filePath))); >catch (IOException e) < e.printStackTrace(); >System.out.println("##### Before split"); System.out.println(contents);

Split string by new line

New line character is different in various operating systems. In Unix, New Mac Environment, lines are just separated by the character «\n«. In Windows Environment lines are just separated by the character «\r\n«. The best way to split a string by new line character using Java regex \r?\n .

System.out.println(«##### split string by new line»); String[] lines = contents.split(«\\r?\\n»); for (String line : lines)

Split string by multiple spaces

To split the string with one or more whitespaces we use the «\s+» regular expression. Let’s see the code snippet below.

System.out.println(«##### Split string by multiple spaces»); String[] fruits = contents.split(«\\s+»); for(String fruit : fruits)

Full source code Split String

package com.jackrutorial; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class SplitStringExample < public static void main(String[] args) < String filePath = "c:/data/fruits.txt"; String contents = null; try < contents = new String(Files.readAllBytes(Paths.get(filePath))); >catch (IOException e) < e.printStackTrace(); >System.out.println("##### Before split"); System.out.println(contents); System.out.println("##### split string by new line"); String[] lines = contents.split("\\r?\\n"); for (String line : lines) < System.out.println(line); >System.out.println("##### Split string by multiple spaces"); String[] fruits = contents.split("\\s+"); for(String fruit : fruits) < System.out.println(fruit); >> >

Console Output

##### Before split Cherry Banana Chico fruit Jackfruit Kiwifruit Orange Pineapple Mango Apple ##### split string by new line Cherry Banana Chico fruit Jackfruit Kiwifruit Orange Pineapple Mango Apple ##### Split string by multiple spaces Cherry Banana Chico fruit Jackfruit Kiwifruit Orange Pineapple Mango Apple

Источник

Читайте также:  Css href link active
Оцените статью