gpt4 book ai didi

android - 在字符串中检测\n 的正则表达式是什么?这里\和n存储在一个字符串的两个不同的单元格中

转载 作者:行者123 更新时间:2023-11-29 14:37:49 25 4
gpt4 key购买 nike

案例 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?

关于android - 在字符串中检测\n 的正则表达式是什么?这里\和n存储在一个字符串的两个不同的单元格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25184051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com