Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 23956
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: 23956
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 found some pretty shells, some sea glass and this lovely tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic