Java 中的行分隔符


字串沒有換行。我們可以透過連線一個換行字串形成兩行。使用 System 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

更新於: 2020 年 6 月 27 日

3 千次 + 瀏覽量

開啟您的 職業生涯

完成課程後即可獲得認證

開始
廣告
© . All rights reserved.