我创建了一个包含以下文本的对象(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]);
}
**
最佳答案
对于这个特定的 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);
输出
E-Mail: custrelations@abc.com
Confirmation Number (PNR):H246FY
注意:\r\n
也应该用于 Windows,而不仅仅是 Scary Wombat
指出的 \n
关于java - 如何在文本之间插入新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25153454/