如何获得依赖于平台的换行符? [英] How do I get a platform-dependent new line character?

查看:38
本文介绍了如何获得依赖于平台的换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Java 中获得依赖于平台的换行符?我不能到处使用 "\n".

How do I get a platform-dependent newline in Java? I can’t use "\n" everywhere.

推荐答案

除了 line.separator 属性,如果您使用的是 java 1.5 或更高版本和 String.format(或其他 <强>格式化方法)你可以使用 %n 如在

In addition to the line.separator property, if you are using java 1.5 or later and the String.format (or other formatting methods) you can use %n as in

Calendar c = ...;
String s = String.format("Duke's Birthday: %1$tm %1$te,%1$tY%n", c); 
//Note `%n` at end of line                                  ^^

String s2 = String.format("Use %%n as a platform independent newline.%n"); 
//         %% becomes %        ^^
//                                        and `%n` becomes newline   ^^

请参阅 Java 1.8 API for Formatter 了解更多详情.

See the Java 1.8 API for Formatter for more details.

这篇关于如何获得依赖于平台的换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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