How to Split String By New Line in Java


In this example, we will obtain the method to split string by new line in Java.

Source Code

package com.beginner.examples;

public class SplitStringExample {

    public static void main(String[] args){

    	String string = "ac9BGnRN,7e3,tgfi";
    	string += "dddrnmm";
		String[] strs = string.split("\r?\n");
		
		int count = 1;
		for(String line:strs) 
		{
			System.out.println("line " + count++ + " : " + line);
		}
    }
}

Output:

line 1 : ac9BG
line 2 : RN,7e3,tgfiddd
line 3 : mm
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments