3

I know that similar questions have been asked several times, but the answers I saw are either workarounds or simply incorrect.

The basic problem is fairly simple: A method gives me a string read from a file and I need to check if this string contains newline.

Now to the tricky part: Wikipedia currently lists eight types of characters or character combinations which may, dependent on the system, denote a newline. So checking for the common \n and \r, an answer, that I often read, is not the way to go. Walking through the string and compare its characters with System.getProperty("line.separator") might also fail, since a possible newline representation is `\r\n', that will trigger this comparison twice, though its only one newline.

However, this has to be possible. What option am I missing?

4
  • Not sure what you mean by this, "that will trigger this comparison twice". Why would it happen twice? But are the files you are reading, generated/created on the same machine/os that you are running this application? Commented Mar 11, 2015 at 15:01
  • I mean, if I check for \r as trigger and for \n as trigger and I'm on an os which gives me \r\n for a newline, a char-by-char comparison will find two newlines, however there has been only one. Also, the files may come from different systems with different os and the application has to run on different systems with different os. Commented Mar 11, 2015 at 18:00
  • But wouldn't you be looking for the occurrence of \r\n as a whole, not both \r and \n. For instance StringUtils.countMatches("ab\r\ncd\r\n", System.getProperty("line.separator")) on a machine that has \r\n as the line separator would return 2. If you don't know the OS the file originated from, I'd say go with Braj's method. Commented Mar 11, 2015 at 18:12
  • If I look for the occurence of \r\n as a whole, I don't find newline on systems that use either only \r or only \n. So I will need a loop that checks first, if \r\n appears, and then, if it does not appear, if any other single-char newline indicator appears. This will result in the necessity to run through the string twice in most cases, what I want to avoid. Commented Mar 12, 2015 at 9:00

2 Answers 2

6

You can use the regex pattern ^(.*)$ together with the modifier Pattern.MULTILINE. A method that checks if a string contains any new line character would look like this:

static boolean containsNewLine(String str) {
    Pattern regex = Pattern.compile("^(.*)$", Pattern.MULTILINE);
    return regex.split(str).length > 0;
}

It splits the string in n parts, depending on the number of newline characters. If the string contains any newline character, the length will be greater than 0.

Normally ^ and $ will match only the beginning and the end of the string, but you can change this behavior by passing Pattern.MULTILINE. From the docs:

In multiline mode the expressions ^ and $ match just after or just before, respectively, a line terminator or the end of the input sequence. By default these expressions only match at the beginning and the end of the entire input sequence.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for being late, I haven't had the time to implement anything during the last weeks. However, this solution works great, if I use only "^" as pattern instead of "^(.*)$", which does not work at all. Since this is also completely platform independent, it's definitely the best way to do this.
1

You can try with Regex pattern \r?\n where \r is optional.

sample code:

    String str = "abc\r\nlmn\nxyz";
    Pattern pattern = Pattern.compile("\r?\n");
    Matcher matcher = pattern.matcher(str);
    int count=0;
    while(matcher.find()){
        count++;
    }
    System.out.println(count);    // prints 2

1 Comment

This handles only two out of eight possible newline indicators. It finds CR+LF and a single LF, but it wont find a single CR, VT, FF, NEL, LS, or PS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.