\n JAVA 中有多少个字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6326325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:23:06  来源:igfitidea点击:

how many characters in \n JAVA?

java

提问by kitokid

just a quick question.

只是一个简单的问题。

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

我想知道JAVA 中\n(新行)有多少个字符?

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

因为我需要在一个字符串中放入 160 个字符,等等。

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

那么.. 附加到我的 StringBuffer 时,我应该为 \n 考虑多少个字符?

Thanks

谢谢

回答by Ignacio Vazquez-Abrams

'\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.

'\n'在 Java 内部表示时,它本身是一个字符,但在与外部系统接口时,它可以用 1 到 8 个字节之间的任何位置表示。一些系统和协议用 表示一个换行符\r\n,它是 2 个字符。编码也很重要,因为它可能导致每个字符使用 1、2 或 4 个字节。

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.

如果没有更多关于字符将在什么系统或协议上发送的信息,就不可能给出完全准确的答案。

回答by Vik Gamov

1 character.

1 个字符。

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

output 160

输出 160

回答by Petar Ivanov

'\n' is just a single character.

'\n' 只是一个字符。

回答by AjayR

\n should be single character only.

\n 只能是单个字符。

回答by Peter Lawrey

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

该属性System.getProperty("line.separator")是一个系统相关的换行字符串。这可以是一两个字符。\n是换行(单个)字符,它可能与换行符相同,但这取决于平台。

回答by ameyume

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

'\n', '\r', '\t', '\0' 它们在 java 和 c/c++ 以及其他语言中都是一个字符。

回答by M.J.

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

\n在java中只对应一个字符。如果您需要检查,那么此代码可能会有所帮助。

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