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?
\ras trigger and for\nas trigger and I'm on an os which gives me\r\nfor 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.\r\nas a whole, not both\rand\n. For instanceStringUtils.countMatches("ab\r\ncd\r\n", System.getProperty("line.separator"))on a machine that has\r\nas 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.\r\nas a whole, I don't find newline on systems that use either only\ror only\n. So I will need a loop that checks first, if\r\nappears, 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.