Android 用于检测字符串中\n的正则表达式是什么?此处\和n存储在字符串的两个不同单元格中
案例1:我正在读取文件中的一行。示例:Android 用于检测字符串中\n的正则表达式是什么?此处\和n存储在字符串的两个不同单元格中,android,regex,special-characters,Android,Regex,Special Characters,案例1:我正在读取文件中的一行。示例:“出现错误。\n是否继续?” 问题:当我在安卓系统的对话框中显示此内容时,我看到显示器中出现了\n,“是否继续?”没有显示在新行中。 调试时,我发现\和n存储在(字符串的)单独的单元格中 案例2:另一方面,如果在代码库中定义上述字符串,而不是从文件中读取,则\n似乎存储在单个单元格中。我没有看到\n,我在新行中看到“您想继续吗?” string = string.replace("regex", System.getProperty("line.separa
“出现错误。\n是否继续?”
问题:当我在安卓系统的对话框中显示此内容时,我看到显示器中出现了
\n
,“是否继续?”
没有显示在新行中。调试时,我发现
\
和n
存储在(字符串的)单独的单元格中
案例2:另一方面,如果在代码库中定义上述字符串,而不是从文件中读取,则\n
似乎存储在单个单元格中。我没有看到\n
,我在新行中看到“您想继续吗?”
string = string.replace("regex", System.getProperty("line.separator"));
关于案例1:我现在要做的是从字符串中删除\
和n
,并添加新行
string = string.replace("regex", System.getProperty("line.separator"));
在这种情况下,检测
\
和n
的正则表达式是什么?您应该转义反斜杠字符以指定文字反斜杠。因此,类似于以下内容的内容应该会起作用:
String s = "There is an error. \\n Do you want to continue?";
s = s.replace("\\n", System.getProperty("line.separator");
System.out.println(s);
应转义反斜杠字符以指定文字反斜杠。因此,类似于以下内容的内容应该会起作用:
String s = "There is an error. \\n Do you want to continue?";
s = s.replace("\\n", System.getProperty("line.separator");
System.out.println(s);
如果您将以下内容视为输出:
There is an error. \n Do you want to continue?
下面的方法应该有效,使用双转义符匹配文本
String s = "There is an error. \\n Do you want to continue?";
s = s.replace("\\n", "\n");
System.out.println(s);
// There is an error.
// Do you want to continue?
或
如果您将以下内容视为输出:
There is an error. \n Do you want to continue?
下面的方法应该有效,使用双转义符匹配文本
String s = "There is an error. \\n Do you want to continue?";
s = s.replace("\\n", "\n");
System.out.println(s);
// There is an error.
// Do you want to continue?
或
你试过什么?如果按字面解释,我认为
\n
或者\\n
应该可以解决这个问题。这就是string=string.replace(\\n),System.getProperty(“line.separator”)代码>你试过什么?如果按字面解释,我认为\n
或者\\n
应该可以解决这个问题。这就是string=string.replace(\\n),System.getProperty(“line.separator”)代码>