如何附加「\ n」,從字面上看,不像特殊字符。如何附加字面上與StringBuffer?
我不會來實現這一目標:
StringBuffer st = new StringBuffer;
st.append(text1);
st.append("\n");
st.append(text2);
和輸出字符串是:
text1 \n text2
不
text1
text2
如何附加「\ n」,從字面上看,不像特殊字符。如何附加字面上與StringBuffer?
我不會來實現這一目標:
StringBuffer st = new StringBuffer;
st.append(text1);
st.append("\n");
st.append(text2);
和輸出字符串是:
text1 \n text2
不
text1
text2
StringBuffer st = new StringBuffer();
st.append("Hello");
st.append("\\n"); //<- the first \ escapes the second
st.append("World");
st.append("\\n");
逃亡「\」與其他\:
st.append(" \\n ");
您需要轉義反斜線,如果你想打印一個反斜槓:
st.append("\\n");
你要逃離逃離符號\
。這意味着你必須追加"\\n"
如果不需要同步並且需要轉義特殊字符(如前面其他答案中提到的),請檢查以使用StringBuilder以獲得更好的性能。