Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Split

 
Rajith Gamage
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trailing empty strings are not included using the split method that takes regex as argument
 
Rajith Gamage
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 |.
 
Rajith Gamage
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic