简体   繁体   中英

how many characters in \n JAVA?

just a quick question.

I wonder how many characters count for \n (new line ) in JAVA?

Because I need to fit 160 chars in one String,etc.

So.. How many chars should I consider for \n when appending to my StringBuffer?

Thanks

'\n' itself is one character when represented internally in Java, but when interfacing with external systems it can be represented by anywhere between 1 and 8 bytes. Some systems and protocols represent a newline with \r\n , which is 2 characters. And the encoding matters as well, since it can cause each character to use 1, 2, or 4 bytes.

Without more information on what system or protocol the characters are going to be sent on it is not possible to give a completely accurate answer.

1 character.

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

output 160

'\n' is just a single character.

\n should be single character only.

The property System.getProperty("line.separator") is a system dependant new-line String. This can be one or two characters. \n is the line feed (single) character which could happen to be the same as a new line, but this is platform dependant.

\n in java corresponds to only one character. If you need to check then might this code will help.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM