I have string like
\"I am a boy\".
I want to print like this way
\"I
am
a
boy\".
Can anybody help me?<
If you want to have your code os-unspecific you should use println for each word
System.out.println("I");
System.out.println("am");
System.out.println("a");
System.out.println("boy");
because Windows uses "\r\n" as newline and unixoid systems use just "\n"
println always uses the correct one
System.out.println("I\nam\na\nboy");
This works It will give one space character also along before enter character
Go for a split.
String string = "I am a boy";
for (String part : string.split(" ")) {
System.out.println(part);
}
To make the code portable to any system, I would use:
public static String newline = System.getProperty("line.separator");
This is important because different OSs use different notations for newline: Windows uses "\r\n", Classic Mac uses "\r", and Mac and Linux both use "\n".
Commentors - please correct me if I'm wrong on this...
What about %n
using a formatter like String.format()
?:
String s = String.format("I%nam%na%nboy");
As this answer says, its available from java 1.5 and is another way to System.getProperty("line.separator")
or System.lineSeparator()
and, like this two, is OS independent.
you can use <br>
tag in your string for show in html pages