• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to split String by carriage return independently on OS?  RSS feed

 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I have String and I know that it contains cariiage return.
I want to split String by carriage return but regardless of operating system.
I tried
var.split(System.getProperty("line.separator"));
var.split(String.format("%n"));
All this does not work.

Thank you!
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you mean "carriage return" ? Sounds to me like you actually mean end-of-line . I always spit on "\r\n|\r|\n" .
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply!

I mean end of line.
I want to access end-of-line character independently on OS and with it I want to split my String. String is returned by resultSet.getString.
Your example is cool but is valid only for windows, probably.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Volodymyr Levytskyi wrote:
Your example is cool but is valid only for windows, probably.


It is valid for systems that use "\r\n" as end-of-line (Windows). It is valid for systems that use "\r" as end-of-line (Old MACs). It is valid for systems that use "\n" as the end-of-line (Unix like).

It is not normally wise to split based on the system line separator since it restricts one to only process files from the same type of system. I process files from multiple of different systems and don't know in advance what the lend-of-line is going to be.

The BufferedReader.readLine() method uses the same basic line separator; to quote the Javadoc "Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed". Since this method is considered an OS independent method is would seem that my approach is as OS independent as BufferedReader.

Your call.









 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!