2014-12-02 119 views
0
String in = "dr. goldberg offers everything i look for in a general practitioner. he's nice and easy to talk to without being patronizing; he's always on time in seeing his patients; he's affiliated with a top-notch hospital (nyu) which my parents have explained to me is very important in case something happens and you need surgery; and you can get referrals to see specialists without having to see him first. really, what more do you need? i'm sitting here trying to think of any complaint\n\ns i have about him, but i'm really drawing\n a blank."; 

此代碼似乎工作得很好。替換(「 n」,「」)不起作用

in = in.replaceAll("\n", ""); 

但這

for(String temp: review){ 
    String reviews = (StringUtils.substringBetween(temp,"\"text\": \"", "\", \"type\"")).replaceAll("\n", ""); 

    if(reviews.equals(in)){ 
     System.out.println("yes"); 
     System.exit(1); 
    } 
} 

似乎並沒有工作。它不是刪除新的常量,有人可以解釋爲什麼!

(我使用substringBetween獲取文本從一個文件中的一部分)

+5

什麼呢'StringUtils.substringBetween(溫度, 「\」 文本\ 「:\」 「 」\「,\ 」類型\「」))'演算值爲在運行時? – 2014-12-02 22:06:15

+0

@JigarJoshi - 它評估到相同的字符串asinput2但新的線常量,替換似乎並沒有工作 – 2014-12-02 23:03:25

+0

我也檢查這兩個字符串的平等,使用input.equals(評論),它應評估爲真,但事實並非如此。 – 2014-12-02 23:04:25

回答

0

看一看下面的代碼:

String sample="This string has new line \\n " + "New line is here if \\n \n is working"; 
    System.out.println("Original String: " + sample); //replacing \n or newline character so that all text come in one line 
    System.out.println("Escaped String: " + sample.replaceAll("\n", "")); 

或者,您可以使用系統屬性刪除換行符如下圖所示:

String text = readFileAsString("words.txt"); 
text = text.replace(System.getProperty("line.separator"), "");