如何获得与平台相关的新行字符? [英] How do I get a platform-dependent new line character?

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

问题描述

如何在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 了解更多详情。

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

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