0

As per the question, is there a better/more robust way of checking if a String contains a newline from any platform in Java, or is below the recommended approach?

if (myString.contains("\n") || myString.contains("\r") || myString.contains("\r\n")) {

}
user2586917
  • 762
  • 1
  • 9
  • 26
  • 7
    Well, that's definitely redundant, so at the very least you can leave out `myString.contains("\r\n")` – jhobbie Jul 02 '14 at 13:33
  • 1
    possible duplicate of [Split Java String by New Line](http://stackoverflow.com/questions/454908/split-java-string-by-new-line) – Harry Lime Jul 02 '14 at 13:37
  • is there a platform where `\r` is a line separator? – assylias Jul 02 '14 at 13:42
  • I guess if the split works for that then the `Pattern.match` from the linked question should work for it too to detect them. – EpicPandaForce Jul 02 '14 at 13:44
  • Your question is slightly ambiguous. Are you receiving your strings from various platforms and need to find newlines? Or are you running your code on various platforms and need to find the local newline? – Duncan Jones Jul 02 '14 at 14:01
  • 1
    @assylias Mac 9 [apparently](http://stackoverflow.com/questions/454908/split-java-string-by-new-line#comment6716785_454913). – Duncan Jones Jul 02 '14 at 14:02
  • 1
    This can never work. It can work for ASCII(-like) latin alphabet strings that are from MS-DOS, Unix or Legacy MacOS but is unlikely to work, on Arabic, Korean and Chinese files, that may have origins on systems which use other characters (like NULL) as line terminators. You will need to know up front, what kind of data you accept as valid. – Mikkel Løkke Jul 02 '14 at 14:47
  • @HarryLime There may be dupes, but that one is quite clearly a different question, even if the answer boils down to the same thing (which means search engines won't find it by using the title of this question, for instance). – Maarten Bodewes Jul 13 '14 at 13:30

2 Answers2

1

Looking for '\r' and '\n' in the string should work on at least Windows, Unix and old MAC, for Western style languages. You could actually just look for '\n' if you want to support modern Apple (OS X onwards), Linux and Windows machines.

If you want to have support for machines you dug up from a garbage heap you may have to look for other characters as well. Furthermore, Asian languages may not use line breaks or use other whitespace as replacement character.

Also beware of other methods of formatting text, if those may apply. <br> may also indicate a line break if HTML can be assumed.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
-1

Maybe this is a universal solution to your problem:

if (myString.contains(System.getProperty("line.separator"))) {
//your code here
}

It uses the system properties to find the line separator used on the user's system.

Thibstars
  • 1,085
  • 1
  • 9
  • 30
  • Of course this wouldn't work for any platform, just the current one, but it's nice and elegant. – Mikkel Løkke Jul 02 '14 at 14:30
  • Why wouldn't it work for any platform? It works fine on every platform able to run Java, that's the only platforms relevant in this case. – Thibstars Jul 02 '14 at 15:07
  • 3
    @Thibstars The op wants to be able to identify *any* types of line separators in a string - your approach would only identify the line separators *of the current platform*. – assylias Jul 02 '14 at 17:24
  • Thank you very much! That makes a lot of sense! :) – Thibstars Jul 02 '14 at 19:54