如何在文本之间插入newLine

Vis*_*hnu 1 java string

我创建了一个包含以下文本的对象(new1)

电子邮件:custrelations@abc.com确认号码(PNR):H246FY

输出应显示在两个单独的行中,如下所示:

电子邮件:custrelations@spicejet.com

确认号码(PNR):H246FY

创建了以下逻辑,但在打印输出时没有使用".com"

**

String[] inputSplitNewLine =new1.split(".com");
for(int i=0;i<inputSplitNewLine.length;i++){ 
     System.out.println(inputSplitNewLine[i]);
 }
Run Code Online (Sandbox Code Playgroud)

**

Cod*_*roc 7

对于此特定,String您可以使用#replace方法.

  String str="E-Mail: custrelations@abc.comConfirmation Number (PNR):H246FY";
  str=str.replace(".com", ".com\n");
  //str=str.replace(".com", ".com\r\n");
  System.out.println(str);
Run Code Online (Sandbox Code Playgroud)

OUTPUT

E-Mail: custrelations@abc.com
Confirmation Number (PNR):H246FY
Run Code Online (Sandbox Code Playgroud)

注意:也\r\n应该用于Windows,而不仅仅是\n指出Scary Wombat.