• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

split() method problem

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

Sometimes, I cannot understand the results of the split() of String class.



I guessed the result would be:
count 2
>abc<
>abc<

But, I was wrong.

The result is:

C:\>java SplitTest
count 4
><
><
>abc<
>abc<

How does the split() method split the string ? Thank you.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The argument to split() is a regular expression pattern. Yours is a simple one consisting of exactly one space character. Look again at the string you want to split and carefully mark all the single space characters. What you get is what's between those single space characters. Two adjacent single space characters have an empty string between them.
 
author
Posts: 23923
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
Also notice that even though your orig string has spaces in the end, the split doesn't yield any extra zero-length strings in the results. This is because the split(regex) method calls the split(regex, limit) method with a limit of zero. A limit of zero means to not limit the number of splits, but also to *not* return any trailing zero-length matches.

Henry
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
consider that

String s = ",aa,aaa,aa,,,,";
out.println(s.split(",").length);//4

String s = ",aa,aaa,aa,,,,";
out.println(s.split(",",0).length);//4

String s = ",aa,aaa,aa,,,,";
out.println(s.split(",",10).length);//8
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:
Also notice that even though your orig string has spaces in the end, the split doesn't yield any extra zero-length strings in the results. This is because the split(regex) method calls the split(regex, limit) method with a limit of zero. A limit of zero means to not limit the number of splits, but also to *not* return any trailing zero-length matches.

Henry



Interesting, I did not know that. I tried running the original program with an explicit limit of 10 and, indeed, it tacks on another "><" making count equal to 5.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get these trailing zero strings also with a negative limit number:


-->
0
4


Bu.
 
reply
    Bookmark Topic Watch Topic
  • New Topic