• 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

Pattern Split basic Question

 
Ranch Hand
Posts: 42
Android Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a basic query about Java Pattern class..

Following line of code prints 2



whereas, below code prints zero. Why it's not splitting and giving 2 element array?




String spit method also does the same thing...

Thanks
Nazzy
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is because . (dot) in the pattern matches any character.
Input string is "2.3", the dot in the pattern matches all three characters in the inupt string,
so the output should contain 4 empty strings.
But according to the javadoc:
http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html#split%28java.lang.CharSequence%29

This method works as if by invoking the two-argument split method with the given input sequence and a limit argument of zero.
Trailing empty strings are therefore not included in the resulting array.


Therefore in this case the split method returns an empty array.

Change your pattern to "\\." and you will get desired results.
 
nazzy khan
Ranch Hand
Posts: 42
Android Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ireneusz

Cheers
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic