为什么我不能比较这个炭?

Adr*_*min 0 java

我有一个方法用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;
Run Code Online (Sandbox Code Playgroud)

为什么这段代码text.charAt(i)=='\'无效?

Rei*_*eus 7

\是一个特殊字符,用于表示诸如\n和之类的托架控制字符的开始\t.它应该被转义为代表反斜杠字符本身

text.charAt(i) == '\\'
Run Code Online (Sandbox Code Playgroud)