Java String new line

Md. Rashedul Hasan picture Md. Rashedul Hasan · Oct 20, 2011 · Viewed 929.3k times · Source

I have string like

"I am a boy".

I want to print like this way

"I 
am 
a
boy".

Can anybody help me?

Answer

Prince John Wesley picture Prince John Wesley · Oct 20, 2011
System.out.println("I\nam\na\nboy");

System.out.println("I am a boy".replaceAll("\\s+","\n"));

System.out.println("I am a boy".replaceAll("\\s+",System.getProperty("line.separator"))); // portable way