How do I use System.getProperty("line.separator").toString()?

I have a Tab-delimited String (representing a table) that is passed to my method. When I print it to the command line, it appears like a table with rows:

http://i.stack.imgur.com/2fAyq.gif

The command window is correctly buffered. My thinking is that there is definitely a new line character before or after each row.

My problem is that I want to split up the incoming string into individual strings representing the rows of the table. So far I have:

private static final String newLine = System.getProperty("line.separator").toString();
private static final String tab = "\t";
private static String[] rows;
...

rows = tabDelimitedTable.split(newLine);    //problem is here

System.out.println();
System.out.println("################### start debug ####################");

System.out.println((tabDelimitedTable.contains(newLine)) ? "True" : "False");

System.out.println("#################### end debug###################");
System.out.println();

output:

################### start debug ####################
False
#################### end debug###################

Obviously there is something in the string telling the OS to start a new line. Yet it apparently contains no newline characters.

Running the latest JDK on Windows XP SP3.

Any Ideas?

Try

rows = tabDelimitedTable.split("[" + newLine + "]");

This should solve the regex problem.

Also not that important but return type of

System.getProperty("line.separator")

is String so no need to call toString().

getproperty() and getproperties() methods of System Class in Java , The current System properties are returned as Properties object for use by the getProperties() method. If no such set of properties is present, a set of system is  A derived type declares a property that hides an inherited property with the same name, by using the new modifier ( Shadows in Visual Basic). To resolve the ambiguity, use the GetProperty (String, BindingFlags) method overload and add the BindingFlags.DeclaredOnly flag to restrict the search to members that are not inherited.

Java System.getProperty vs System.getenv, setProperty( "log_dir" , "/tmp/log" );. We can pass our own properties or configurations values to the application using the propertyName command  The java.lang.System.getProperties() method determines the current system properties. The current set of system properties for use by the getProperty(String) method is returned as a Properties object. If there is no current set of system properties, a set of system properties is first created and initialized.

Try BufferedReader.readLine() instead of all this complication. It will recognize all possible line terminators.

Java.lang.System.getProperty() Method, The java.lang.System.getProperty(String key) method gets the system property indicated by the specified key. Declaration. Following is the declaration for java  As we know we can use System.getProperty("user.home") method in java and it returns some specific path. Can we do this in Ext JS to get the same path as like java does, if not then what is the way to

On Windows, line.separator is a CR/LF combination (reference here).

The Java String.split() method takes a regular expression. So I think there's some confusion here.

Java.lang.System.getProperties() Method, The java.lang.System.getProperties() method determines the current system properties. The current set of system properties for use by the getProperty(String)​  To modify the existing set of system properties, use System.setProperties. This method takes a Properties object that has been initialized to contain the properties to be set. This method replaces the entire set of system properties with the new set represented by the Properties object.

I think your problem is that String.split() treats its argument as a regex, and regexes treat newlines specially. You may need to explicitly create a regex object to pass to split() (there is another overload of it) and configure that regex to allow newlines by passing MULTILINE in the flags param of Pattern.compile(). Docs

Java System.getProperty(), We can use System.getProperty() method to find out the Operating System information, user home directory, Java runtime version, path separator, line separator,  You can retrieve all the system properties via System.getProperties() or you can also retrieve individual property via System.getProperty(key). Please note that Access to system properties can be restricted by the Java security manager and policy file. By default, Java programs have unrestricted access to all the system properties.

Java System Properties, Java maintains a set of system properties for its operations. getProperties() or you can also retrieve individual property via System. You didn't mentioned a real world example in which cases I can use these existed or custom defined  Using System.getProperty() The Java platform uses a Properties object to provide information about the local system and configuration and we call it System Properties. System Properties include information such as the current user, the current version of the Java runtime, and file path-name separator.

Java - System.getProperty() Examples, Returns the Java system property indicated by the specified key. public static String getProperty(String key  Java System.getProperty method is very useful to find out the operating system information. We can use System.getProperty () method to find out the Operating System information, user home directory, Java runtime version, path separator, line separator, user working directory, Java home, Java classpath, and various other useful information.

What is the difference between System.getProperty and properties , When you use getProperty on System you get actual properties. The Property class is basically a glorified hash table. You can completely define it  System.getProperty("line.separator") returns the OS dependent line separator. On Windows it returns "\r ", on Unix " ". So if you want to generate a file with line endings for the current operating systems use System.getProperty("line.separator") or write using a PrintWriter.

Comments
  • Why .toString()? It already is a String, unless it is null, in which case you get a NullPointerException.
  • Yes, I just wanted to make sure that I was passing a string instead of a character to .split(). I should have read the documentation on the .getProperty() method, but writing .toString() was faster than opening up my browser lol.
  • Thanks, this worked. I guess I just read the documentation for the split() method and saw that it took a String, not understanding the difference between a regex and a String.
  • If this is Windows and newline is "\r\n", this will in fact split between the \r and \n creating false empty strings.
  • Thank you for your time to answer. I tried one of the other solutions and it worked (it was faster than setting up the scanners in the right places). Since this is only a small portion of my java program, and since I know exactly what the input will be (it's not an arbitrary input text file), I can assume the default newline character. I've looked at the other method that returned this input string, and it uses the platform default character. Thanks for all of your help though.
  • I was parsing outlook pst email headers in linux and the ^M comment helped me understand the output of cat -A. Definitely didn't want the line.separator property in my case.
  • The MULTILINE flag only applies to when you are using the start/end flags (^ and $) in your regex.
  • The MULTILINE flag also causes the "." character to recognize line separators as a match.
  • @James: nope, according to the specs (and according to my testing) MULTILINE "(?m) does not cause the "." character to match line separators. That would be the DOTALL flag "(?s)"
  • Wow... "Responders" sounds so cool. I'm going to use that from now on.
  • I originally wanted this java program to run on a Mac/Linux as well. hence the System.getProperty() method.
  • You could still potentially have input which includes non-system-default line separators. This regex will catch all combinations regardless of the platform and input.