0

I need to detect a new paragraph with one word from variable which was initialized be reading a text from a web site. For instance, a variable contains (it is printing this way):

hello

Are you sure about...

The resulting string must be

Are you sure about...

I do

String arr[] = message.split("\r");
StringBuilder sb = new StringBuilder();
for (String el: arr) {
    String[] temp = el.split(" ");
    if(temp.length > 1){
        sb.append(el).append("\r");
    } else {
        continue;
    }
}

But for some reason in production it does not do the job so the first word is still present in resultin string. What is the reason?

rozerro
  • 5,787
  • 9
  • 46
  • 94
  • 3
    Perhaps your production system isn't using `"\r"` for its line breaks? – azurefrog Aug 29 '16 at 18:16
  • Carriage return isn't newline. Carriage return simply goes back to the start of the line - the same line – smac89 Aug 29 '16 at 18:16
  • 1
    Perhaps a review of this related question would help - http://stackoverflow.com/questions/247059/is-there-a-newline-constant-defined-in-java-like-environment-newline-in-c – CodeMonkey1313 Aug 29 '16 at 18:19
  • Are you perhaps developing on Windows and your production on Linux or Mac? Windows uses CRLF for line endings (`\r\n`) and Linux/Mac use simply LF (`\n`). Like CodeMonkey1313 suggested try looking at this, [`System#lineSeparator()`](http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#lineSeparator%28%29) – Captain Man Aug 29 '16 at 18:26

1 Answers1

0

New line character should be used instead of carriage. A simplified version of your code:

String arr[] = message.split("\n");
StringBuilder sb = new StringBuilder();
for (String el : arr) {
    if (el.trim().split(" ").length > 1) {
        sb.append(el).append("\n");
    }
}
System.out.println(sb.toString());
Shahid
  • 2,288
  • 1
  • 14
  • 24