Java - 新行字符问题 [英] Java - New Line Character Issue

查看:124
本文介绍了Java - 新行字符问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的代码,

String str = " " ; 

while(  cond ) {

  str = str + "\n" ;

}

现在,我不知道为什么当时的打印时,输出字符串不会打印换行符。但是,当我添加任何其他字符(str = str +c)时,它正确打印。任何人都可以帮助我,如何解决这个问题以及为什么会发生这种情况?

Now, I don't know why at the time of printing, the output string is not printing the newline character. However, when I add any other character like ( str = str + "c"), it is printing properly. Can anybody help me, how to solve this problem and why this happening ?

推荐答案

看起来你正试图运行上面的代码在Windows上。那么行分隔符或新行在Windows上是不同的('\\ n'')和Unix风格('\ n')。

Looks like you are trying to run the above code on Windows. Well the line separator or new line is different on Windows ( '\r\n' ) and Unix flavors ('\n').

所以,改为硬编码并使用'\ n'作为新行。尝试从系统中获取新行,例如:

So, instead of hard coding and using '\n' as new line. Try getting new line from the system like:

String newLine = System.getProperty("line.separator");
String str = " " ; 

while(  cond ) {

  str = str + newLine ;

}

这篇关于Java - 新行字符问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆