String Java String

I have a line like "I'm a boy." I want to print in this way "I (\ n new line) am (\ n new line) a (\ n new line) boy".

Can someone help me?

+81
java string newline
Oct. 20 2018-11-11T00:
source share
14 answers
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 
+107
Oct. 20 '11 at 9:21 a.m.
source share

You can also use System.lineSeparator() :

 String x = "Hello," + System.lineSeparator() + "there"; 
+66
Jul 23 '15 at 23:46
source share

There are several ways to do this. I mention 2 simple ways.

  • A very simple way, as shown below:

     System.out.println("I\nam\na\nboy"); 
  • This can also be done using concatenation, as shown below:

     System.out.println("I" + '\n' + "am" + '\n' + "a" + '\n' + "boy"); 
+15
Apr 22
source share

Example

 System.out.printf("I %n am %na %n boy"); 

Exit

 I am a boy 

Explanation

It's better to use %n as an OS-independent newline instead of \n , and it's easier than using System.lineSeparator()

Why use %n , because in each OS a new line refers to a different character set,

 Unix and modern Mac : LF (\n) Windows : CR LF (\r\n) Older Macintosh Systems : CR (\r) 

LF is an abbreviation of Line Feed and CR is an abbreviation of Carriage Return . Control characters are written inside brackets. Thus, in each OS, a new line means something specific to the system. %n is agnostic, it is portable. It stands for \n on Unix systems or \r\n on Windows systems and so on. Thus, do not use \n , but use %n .

+14
Jun 28 '16 at 13:41
source share

Try:

 System.out.println("I\nam\na\nboy"); 
+12
Oct 20 '11 at 9:20
source share

To port the code to any system, I would use:

 public static String newline = System.getProperty("line.separator"); 

This is important because different OSs use different notations for the new line: Windows uses "\ r \ n", Classic Mac uses "\ r", and Mac and Linux use "\ n".

Comments - please correct me if I am wrong about this ...

+8
May 24 '15 at 12:43
source share

\n used to create a single line;

Example:

 System.out.print("I" +'\n'+ "am" +'\n'+ "boy"); 

Result:

 I am boy 
+6
Jul 31 '15 at 7:29
source share

If you just want to print a new line in the console, you can use "\ n" for new lines.

If you want to split text in swing components, you can use html:

 String s = "<html>first line<br />second line</html>"; 
+5
Oct 20 '11 at 9:21 a.m.
source share

If you want your os-unspecific code, you must 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" because newlines and unixoid systems only use "\ n"

println always uses the correct

+5
Oct. 20 '11 at 9:25 a.m.
source share

What about %n using formatting like String.format() ?:

 String s = String.format("I%nam%na%nboy"); 

As this answer says that it is available from java 1.5 and is in another way System.getProperty("line.separator") or System.lineSeparator() , and, like these two, is OS independent .

+2
Jul 30 '15 at 21:58
source share

Full example program with a funny twist:

Open a new blank document and save it as %yourJavaDirectory%/iAmABoy/iAmABoy.java . "iAmABoy" is the name of the class.

Paste the following code and read it. Remember, I'm new, so I appreciate all the feedback!

 //The class name should be the same as your Java-file and directory name. class iAmABoy { //Create a variable number of String-type arguments, "strs"; this is a useful line of code worth memorizing. public static void nlSeparated(String... strs) { //Each argument is an str that is printed. for (String str : strs) { System.out.println(str); } } public static void main(String[] args) { //This loop uses 'args' . 'Args' can be accessed at runtime. The method declaration (above) uses 'str', but the method instances (as seen below) can take variables of any name in the place of 'str'. for (String arg : args) { nlSeparated(arg); } //This is a signature. ^^ System.out.print("\nThanks, Wolfpack08!"); } } 

Now, in the terminal / cmd, go to %yourJavaDirectory%/iAmABoy and type:

 javac iAmABoy.java java iAmABoy I am a boy 

You can replace args I am a boy with something!

+1
May 17 '14 at 2:10
source share

you can use the <br> tag in your line to display on html pages

+1
Feb 10 '16 at 10:05
source share

System.out.println ("I \ Nam \ on \ nboy");

It works. It will also give one space before entering a character.

0
Aug 05 '14 at 11:04
source share

Go to split.

 String string = "I am a boy"; for (String part : string.split(" ")) { System.out.println(part); } 
0
Feb 10 '16 at 10:24
source share



All Articles