• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Tokenizing with String.split()

 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks,

In page 503 of the Kathy Sierra book, there is the following sample code:


With this invocation:

java SplitTest "ab5 ccc 45 @" "\d"



It gives the following result:

count 4
>ab<
> ccc <
><
> @<



My question is, how can the count be 4? My understanding is that the value being assigned to the token array should be the number of elements left after the digits are removed. Hope someone can advise. Thank you.
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Paterson wrote:
My question is, how can the count be 4? My understanding is that the value being assigned to the token array should be the number of elements left after the digits are removed. Hope someone can advise. Thank you.



The example actually prints the four elements -- so you know what the application says the four are. How many elements do you think it should there be? And what should the elements be?

Henry
 
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also interesting result of:

i expected


count 2
><
><

, but result is

count 0


and


expected output:

count 2
>/<
><


but get:

count 2
><
>/<

 
Henry Wong
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sergej Smoljanov wrote:I also interesting result of:

i expected


count 2
><
><

, but result is

count 0




The String split() method uses the one-argument split() method of the Pattern class. See... link

And notice the mention on "trailing empty strings" in the result.

Hope this helps,
Henry
 
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic