2016-06-14 192 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