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.";

This code seems to be working perfectly fine.

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

but this

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

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

does not seem to work. It is not removing new line constant, Can someone please explain why!

(I am using substringBetween to get a part of text from a file)

Sentry
  • 4,102
  • 2
  • 30
  • 38
  • 5
    what does `StringUtils.substringBetween(temp,"\"text\": \"", "\", \"type\""))` evalutes to at runtime ? – jmj Dec 02 '14 at 22:06
  • @JigarJoshi - it evaluates to the same string asinput2 but with new line constant, the replace doesn't seem to be working – shreya seshu Dec 02 '14 at 23:03
  • also i am checking equality of these two strings, using input.equals(reviews), it should evaluate to true, but it is not. – shreya seshu Dec 02 '14 at 23:04
  • also note that you are using `replace()` not `replaceAll()` so it won't replace all but first – jmj Dec 02 '14 at 23:04
  • @RuslanOstafiychuk - i have tried that and it still doesn't work. – shreya seshu Dec 02 '14 at 23:06
  • @JigarJoshi - tried that also, still no luck. if i use replace("\\n",""); it replaces the new line constant but input.equals(reviews) doesnt evaluate to true. – shreya seshu Dec 02 '14 at 23:14
  • can you post reproducible code ? – jmj Dec 02 '14 at 23:15
  • would you mind editing it in question to make it readable ? – jmj Dec 02 '14 at 23:19
  • @JigarJoshi - done that,please let me know if you can find some solution – shreya seshu Dec 02 '14 at 23:23
  • again this code is not reproducible, just `-A 5 -B 4` of main code, can you create a simple main class and reproduce it ? – jmj Dec 02 '14 at 23:25
  • that is the problem here, if i directly write the sentence myself and heck for equality, it seems to be working, but when i am extracting the same sentence from the file and trying to remove new line constant it is not working@JigarJoshi – shreya seshu Dec 02 '14 at 23:28
  • @shreyaseshu Have you considered the possibility that the line terminator is `\r\n`, not just `\n`? – user207421 Dec 03 '14 at 00:04

1 Answers1

0

Have a look at the following code:

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", ""));

Alternatively, you can use system properties to remove new line character as shown below:

String text = readFileAsString("words.txt"); 
text = text.replace(System.getProperty("line.separator"), "");
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102