[英]newline character indenting? (Java)
我試圖使用\\ n創建一個字符串列表,但每當我添加多個字符串時,字符串縮進到前一個字符串的點,所以示例輸出將是:
0 hello nain
1 test nain
2 huh nain
我不知道為什么這樣做。 這是我創建字符串的代碼:
String[] posts = currentTopic.getMessages("main");
//String[] postsOutput = new String[posts.length];
String postsOutput = "0 " + posts[0];
for(int i = 1; i < posts.length; i++){
postsOutput += "\n" + i + " " + posts[i];
//postsOutput[i] = i + posts[i];
}
sc.close();
return postsOutput;
我也嘗試將\\ n移動到附加的末尾,但結果仍然相同。 任何幫助,將不勝感激。
謝謝
看起來你正處於“\\ n”剛下來的系統(換行)並且你錯過了所需的回車。
這不應該是你應該關心的事情: line.separator
屬性正在適應主機操作系統,因此它的行為類似於System.out.println
。
嘗試\\ r \\ n,即回車和換行。
我認為這是使用String.format的一個很好的例子,因為%n
總是使用系統特定的行分隔符。 在你的循環中寫道:
postsOutput += String.format("%n%d %d", i, posts[i]);
您應該使用屬性System.getProperty("line.separator")
,它保存底層系統換行符。
String newline = System.getProperty("line.separator");
String[] posts = currentTopic.getMessages("main");
//String[] postsOutput = new String[posts.length];
String postsOutput = "0 " + posts[0];
for(int i = 1; i < posts.length; i++){
postsOutput += newline + i + " " + posts[i];
//postsOutput[i] = i + posts[i];
}
sc.close();
return postsOutput;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.