Java中的行分隔符
字符串没有换行符。我们可以通过连接换行字符串将它们分成两行。使用系统lineSeparator获取平台相关的换行符。
以下是一个示例。
示例
public class Demo {
public static void main(String[] args) {
String str = "one" + System.lineSeparator() + "two";
System.out.println(str);
}
}输出结果
one two
让我们来看另一个例子。在基于Linux的系统上,该程序将正常运行。
示例
public class Demo {
public static void main(String[] args) {
String str = System.lineSeparator();
System.out.println((int) str.charAt(0));
}
}输出结果
10