本文目錄一覽:
java識別回車改為換行
Java中可以使用 System.getProperty(“line.separator”) 來識別回車換行符。例如:
String lineSeparator = System.getProperty(“line.separator”);
String text = “This is a line of text” + lineSeparator + “This is another line of text”;
上面的代碼中,lineSeparator 變量包含了當前系統的回車換行符,然後我們可以在拼接字符串時使用它來指定換行符。
JAVA 怎麼樣在輸入的時候識別出回車
java中可以使用buffereader類來獲得控制台輸入的回車鍵,示例如下:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Test {
public static void main(String args[]) throws Exception {
System.out.println(“輸入:”);
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String str = “”;
do {
str = bf.readLine();
if (str.length() == 0) { // 如果輸入的字符串為空,則說明只輸入了一個回車
System.out.println(“輸入的是回車!”);
} else {
System.out.println(“輸入內容是:” + str);
}
} while (str.length() != 0);
}
}
這樣可以在輸入回車後提示“輸入的是回車!”並結束程序
java中如何表示一個回車符
可以使用Java中\n和\r的換行,不過也是有區別的,如下:
1.\r 叫回車 Carriage Return
2.\n 叫新行 New Line
但是都會造成換行,使用System.getProperty(“line.separator”)來獲取當前OS的換行符
java 代碼
1. String userInputString = userInput;
2. userInputString = userInputString.replaceAll ( “\r”, “” );
3. userInputString = userInputString.replaceAll ( “\n”, “\\\\”+System.getPropert(“line.separator”));
原創文章,作者:IMIS,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/139919.html