• 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

How the split() of Strings work?

 
Ranch Hand
Posts: 213
  • 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: 9272
78
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: 9272
78
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: 213
  • 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: 9272
78
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: 9272
78
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:

 
permaculture is giving a gift to your future self. After reading 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