• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

String split method working

 
Ranch Hand
Posts: 391
1
MySQL Database PHP Java
  • 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
 
Marshal
Posts: 71032
291
  • 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.
 
Marshal
Posts: 7850
538
Mac OS X VI Editor BSD Linux
  • 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: 71032
291
  • 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.
 
Bartender
Posts: 7431
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Sheriff
Posts: 22040
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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).
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE 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: 71032
291
  • 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: 10777
71
Hibernate Eclipse IDE 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
 
Remember to always leap before you look. But always take the time to smell the tiny ads:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic