2016-06-14 169 views

回答

0

下面是使用replaceAll()

public class Program 
{ 
    public static void main(String[] args) { 
     String str = "{ 
      'key1': 'a 
      b', 
      'key2':'value2', 
      'key3':'value3' 
      }"; 
     str = str.replaceAll("a\nb", "a\\nb"); 
     System.out.println(str); 
    } 
} 

编辑解决方案:改变replaceAll("\n", "\\n")replaceAll("a\nb", "a\\nb")

+0

谢谢您的回答!我试过这个,它用'\\ n'替换了所有其他换行符,它打破了json –

+0

String.replaceAll()IS正则表达式 – BoDidely