• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

String split method working

 
Ranch Hand
Posts: 391
1
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 8523
615
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 9738
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want multiple adjacent "s" to be treated as one you can write
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Or we might never have existed at all. Freaky. So we should cherish everything. Even this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic