• Post Reply Bookmark Topic Watch Topic
  • New Topic

String split method working  RSS feed

 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It results in below output wouldn't size should be 2


Size :3
pa

d


I was expecting size to be 2 and [pa, d] as output
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you are splitting on a single s. Before the s you have pa and after the last s you have d. But after the first s and before the second s you have something else. Change your line 8 to read as follows:-
System.out.printf("String = \u201c%s\u201d%n", s);
… and you can see what it is.
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also you can try with streams in case you're using most recent Java 8 version (for the next 10 months or so ).
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:. . .
There is a line, line 4, there which alters the semantics of the program and which will give a different output. It will also give you a hint what the event which surprised you so much is.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want multiple adjacent "s" to be treated as one you can write
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Also you can try with streams in case you're using most recent Java 8 version (for the next 10 months or so ).

A bit shorter:
And because you start from Pattern (which is used internally by String.split), you can make the Pattern a private static final field and share one instance among many, many method calls (since Pattern is thread-safe).
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:A bit shorter:

Or even shorter: What Carey said.

Don't get me wrong, I like streams - and @Mahtab: you should definitely read up on them - but overkill for a case like this, I suspect.

But splitAsStream() was new to me, so thanks for that. I wish there was a single place to go to to find ALL the new Stream methods.

Winston
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It isn't overkill. It is a way to fiddle the split method to get the expected result rather than the actual result. The correct output is a 3‑element array and the filter call simply removes the empty middle element.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It isn't overkill. It is a way to fiddle the split method to get the expected result rather than the actual result. The correct output is a 3‑element array and the filter call simply removes the empty middle element.

Yes, I worked that out, new though these new-fangled Streams are to an old fart like me.

What I'm saying is that with Carey's suggestion you don't have to remove anything, and the result is already an array of the correct length.

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