【发布时间】:2013-07-16 11:26:19
【问题描述】:
我有一种用 br 标记替换字符串中所有“\n”实例的方法。我收到未闭合字符文字错误。
public static String replaceLineWithBr(String text){
String result="";
if(text.length()<=1){
return text;
}else{
for(int i=0;i<text.length();i++){
if((text.charAt(i+1)=='n') && (text.charAt(i)=='\')){ //<--- Error line
result=result+text.substring(0,i)+"<br />"+text.substring(i+2,text.length());
}else return text;
}
}
return text;
为什么这个代码text.charAt(i)=='\' 无效?
【问题讨论】:
-
使用 '\n' 作为字符并与之比较,
text.replace("\n", "<br />");
标签: java