2014-12-02 80 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"), "");