情况1:我正在从文件中读取一行。例:"There is an error. \n Do you want to continue?"
问题:当我在android的对话框中显示此消息时,我在显示中看到,\n
并且"Do you want to continue?"
未在新行中显示。
调试时,我观察到\
和n
存储在(字符串的)单独的单元中。
情况2:另一方面,如果不从文件中读取,而是在代码库中定义上述字符串,则\n
似乎将其存储在单个单元格中。我没看到\n
,我"Do you want to continue?"
换行了。
关于情况1:什么我想现在要做的是去除\
并n
从字符串,并添加新的生产线。
string = string.replace("regex", System.getProperty("line.separator"));
什么是检测正则表达式\
,并n
在这种情况下?
如果您看到以下作为输出:
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?
要么
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.
// Do you want to continue?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句