Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

How the split() of Strings work?

 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The documentation at https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String) illustrate six examples. From the six examples, how could the sixth example be explained. Why does a space " " come as part of the result. If two equal characters are placed adjecently, then a space comes as a part of the result(array). How to explain this splitting?

The string "boo:and:foo", for example, yields the following results with these parameters:

Regex Limit Result
: 2 { "boo", "and:foo" }
: 5 { "boo", "and", "foo" }
: -2 { "boo", "and", "foo" }
o 5 { "b", "", ":and:f", "", "" }
o -2 { "b", "", ":and:f", "", "" }
o 0 { "b", "", ":and:f" }

 
Saloon Keeper
Posts: 8008
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not a "space" that is in the resulting array, it is an EMPTY String.
 
Carey Brown
Saloon Keeper
Posts: 8008
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The string "boo:and:foo", for example, yields the following results with these parameters:

Regex Limit Result

If two 'o's appear back to back then there is an EMPTY String in between them.
 
Varuna Seneviratna
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually what happens when splitting  If you split using  then the result is .

When is split using the result is . In first occurrence of "o" the string is split between "b" and "o:and:foo" then when "o" is encountered for the second time "b" and "o:and:foo" is built into {b", "", ":and:foo} .

Why does an empty string gets constructed?
 
Carey Brown
Saloon Keeper
Posts: 8008
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
result isOnce you hit the first 'o' and split the string you've reached the limit of '2' and the next 'o' is not processed and the second array position contains "everything else".

Trying this with a limit of 3:
result is This time, after splitting on the first 'o' we haven't reached our limit so we split on the second 'o'. And because the second was back to back with the first that meant that there was no data in between them, so the EMPTY data is represented by an  EMPTY String. Now the limit has be reached and the third array position contains everything else.
 
Carey Brown
Saloon Keeper
Posts: 8008
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a test program for experimentation:

The output of this was:

 
I can't renounce my name. It's on all my stationery! And hinted in this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic