How many characters are in \ n JAVA?

just a quick question.

I wonder how many characters are specified in \ n (new line) in JAVA?

Because I need to set 160 characters on one line, etc.

So .. How many characters should I consider for \ n when adding to my StringBuffer?

thanks

+6
source share
7 answers

'\n' is itself a single character if it is represented inside Java, but when interacting with external systems, it can be represented anywhere between 1 and 8 bytes. Some systems and protocols are a new line with \r\n , which is 2 characters long. And encoding matters, as this can lead to the fact that each character will use 1, 2 or 4 bytes.

Without additional information about which system or protocol the characters will be sent to it, it is impossible to give a completely accurate answer.

+7
source

1 character.

 String str = "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\n"; System.out.println(str.length()); ; String str = "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\n"; System.out.println(str.length()); 

pin 160

+3
source

'\ n' is just one character.

+2
source

\ n must be only one character.

+2
source

The System.getProperty("line.separator") property System.getProperty("line.separator") is the system line of the new line. It can be one or two characters. \n is a line character (single), which may match a new line, but it depends on the platform.

+1
source

\n in java matches only one character. If you need to check if this code can help.

 String str = "\n"; char[] test = str.toCharArray(); System.out.println(test.length); 
0
source

'\ n,' \ r ',' \ t ',' \ 0 'all of them are one character both in java, and in c / C ++ and other languages.

0
source

Source: https://habr.com/ru/post/890377/


All Articles