aspose file tools*
The moose likes Java in General and the fly likes Java Split Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Split" Watch "Java Split" New topic
Author

Java Split

Rajith Gamage
Greenhorn

Joined: Jun 17, 2010
Posts: 19
In my code I split some string using vertical bar. But using the same code I got two results. Why is that ?

Here is my first code


the result i got is 24.

Here is my 2nd code (Only modification is after ER vertical line there is a space)



the result I got is 25

Why it give two result or am I missing some point ?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Hi Rajith,

I will leave the explanation be given by other ranchers.

I have just added print statements to the given code. It seems that the split ignores the last two "||" since there is nothing between them and the end of the String...

Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
Trailing empty strings are not included using the split method that takes regex as argument
Rajith Gamage
Greenhorn

Joined: Jun 17, 2010
Posts: 19
Harsha Smith wrote:Trailing empty strings are not included using the split method that takes regex as argument


how can I solve it ?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Pass -1 as the second argument to the split method. If I do that in John's example I get 26 for both input strings - that's including the empty string after the last |.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rajith Gamage
Greenhorn

Joined: Jun 17, 2010
Posts: 19
Rob Spoor wrote:Pass -1 as the second argument to the split method. If I do that in John's example I get 26 for both input strings - that's including the empty string after the last |.


that will help .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Split