Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 替换字符串中的子字符串_Android_String - Fatal编程技术网

Android 替换字符串中的子字符串

Android 替换字符串中的子字符串,android,string,Android,String,我有一个字符串userId:344556\\n客户id:233 我想用新行替换子字符串\\n。所以我试过了 String more = strmoreInfo.replace("\\n", "\n"); 我期望的输出是: userId:344556 customerId:233 userId:344556\ customerId:233 但我目前的输出是: userId:344556 customerId:233 userId:344556\ customerId:233 我做错了什么

我有一个字符串
userId:344556\\n客户id:233

我想用新行替换子字符串
\\n
。所以我试过了

String more = strmoreInfo.replace("\\n", "\n");
我期望的输出是:

userId:344556
customerId:233
userId:344556\
customerId:233
但我目前的输出是:

userId:344556
customerId:233
userId:344556\
customerId:233

我做错了什么?

“\”是转义字符。您需要将语句更改为:

String more = strmoreInfo.replace("\\\\n", "\n");

“\”是转义字符。您需要将语句更改为:

String more = strmoreInfo.replace("\\\\n", "\n");

我会尝试你的代码,但我没有发现任何问题,它的作品

public class HelloWorld
{
  public static void main(String[] args)
  {    
    String strmoreInfo="userId:344556\\ncustomerId:233";
    String more = strmoreInfo.replace("\\n", "\n");
     System.out.println(more);
  }
}

我会尝试你的代码,但我没有发现任何问题,它的作品

public class HelloWorld
{
  public static void main(String[] args)
  {    
    String strmoreInfo="userId:344556\\ncustomerId:233";
    String more = strmoreInfo.replace("\\n", "\n");
     System.out.println(more);
  }
}
只需在replace方法的第二个参数中添加空格,例如“\n”

String more = strmoreInfo.replace("\\n", " \n");
只需在replace方法的第二个参数中添加空格,例如“\n”

String more = strmoreInfo.replace("\\n", " \n");
谢谢@Tah:)我错过了谢谢@Tah:)我错过了