📜  Java程序在字符串中打印新行

📅  最后修改于: 2022-05-13 01:55:29.536000             🧑  作者: Mango

Java程序在字符串中打印新行

Java是最强大的编程语言,我们可以通过它来执行许多任务, Java是业界首选的语言。所以它充满了大量的功能。在这里,我们将讨论Java的最佳特性之一,即如何使用Java在字符串中打印新行。

方法:

有很多方法可以在字符串中打印新行,如下所示:

  1. 使用 System.lineSeparator() 方法
  2. 使用平台相关的字符
  3. 使用 System.getProperty() 方法
  4.  使用 %n字符
  5. 使用 System.out.println() 方法

让我们分别详细讨论它们。

方法 1:使用 System.lineSeparator() 方法

例子

Java
// Java program to print a new line in string
// Using System.lineSeparator() method
  
// Main class
class GFG {
    // Main Driver Code
    public static void main(String[] args)
    {
        // Calling the System.lineSeparator() function to
        // print newline in between some specified strings
        String newline = System.lineSeparator();
  
        // Printing new line
        System.out.println("GFG" + newline + "gfg");
    }
}


Java
// Java program to print a new line in string
// Using platform-dependent Newline Character
  
// Main class
class GFG {
    
    // Main Driver Code
    public static void main(String[] args)
    {
        // Using new line Character '\n' to print
        // new line in between strings
        System.out.println("GFG" + '\n' + "gfg");
    }
}


Java
// Java program to print a new line in string
  
class GFG
{
    // Main Driver Code
    public static void main(String[] args)
    {
          
        // Calling System.getProperty() function Over The 
        // parameter for the value of the system property "line.separator",
        // which returns the system-dependent line separator string. 
        String newline = System.getProperty("line.separator");
          
        // Printing new line between two strings
        System.out.println("GFG" + newline + "gfg");
    }
}


Java
// Java program to print a new line in string
// Using %n Newline Character
  
// Importing input output classes
import java.io.*;
  
// Main class
class GFG {
  
    // Main Driver Code
    public static void main(String[] args)
    {
        // Printing new line using new line
        // Character "%n" with the printf() method
        System.out.printf("GFG%ngfg");
    }
}


Java
// Java program to print a new line in string
// Using System.out.println() method
  
// Main class
class GFG {
    
    // Main driver method
    public static void main(String[] args)
    {
        // Printing new line using
        // System.out.println() function
        // over custom string inputs
        System.out.println("GFG");
        System.out.println("gfg");
    }
}


输出
GFG
gfg

方法二:使用平台相关的字符。

例子

Java

// Java program to print a new line in string
// Using platform-dependent Newline Character
  
// Main class
class GFG {
    
    // Main Driver Code
    public static void main(String[] args)
    {
        // Using new line Character '\n' to print
        // new line in between strings
        System.out.println("GFG" + '\n' + "gfg");
    }
}
输出
GFG
gfg

方法 3:使用 System.getProperty() 方法。这里这个函数使用系统属性“line.separator”的值,它返回系统相关的行分隔符字符串。

例子

Java

// Java program to print a new line in string
  
class GFG
{
    // Main Driver Code
    public static void main(String[] args)
    {
          
        // Calling System.getProperty() function Over The 
        // parameter for the value of the system property "line.separator",
        // which returns the system-dependent line separator string. 
        String newline = System.getProperty("line.separator");
          
        // Printing new line between two strings
        System.out.println("GFG" + newline + "gfg");
    }
}

输出:

GFG
gfg

方法 4:使用 %n字符

例子

Java

// Java program to print a new line in string
// Using %n Newline Character
  
// Importing input output classes
import java.io.*;
  
// Main class
class GFG {
  
    // Main Driver Code
    public static void main(String[] args)
    {
        // Printing new line using new line
        // Character "%n" with the printf() method
        System.out.printf("GFG%ngfg");
    }
}
输出
GFG
gfg

方法 5:使用 System.out.println() 方法。

例子

Java

// Java program to print a new line in string
// Using System.out.println() method
  
// Main class
class GFG {
    
    // Main driver method
    public static void main(String[] args)
    {
        // Printing new line using
        // System.out.println() function
        // over custom string inputs
        System.out.println("GFG");
        System.out.println("gfg");
    }
}
输出
GFG
gfg