• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

String split function and "||" String

 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have a String like the following : "A || B || C || D || E || F || G || H";


The expected outcome for this code is : 8
The real outcode is : 37

Is this a bug in the split function in the String object.


Please help
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Khaled Mahmoud:
Is this a bug in the split function in the String object.



No. The parameter you pass to the split method is a regular expression and | is a special character in regular expressions. If you don't want it to be regarded as a special character you need to escape it. Note however that the escape character (\) is a special character in Java Strings, so you need to escape that as well. Try
 
Sheriff
Posts: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Joanne said, | is a special character in regular expressions. It means choice. Therefore, || means "empty string or empty string or empty string", in other words: empty string.

Now you may think: that would lead to 36 empty strings, because there are only 36 characters: one empty string before each character. The magic here is, there is also an empty string at the end that matches. Hence 37.

You might want to extend your expression to "\\s*\\|\\|\\s*" to include the spaces (whitespace) as well. \s (unescaped) means all whitespace characters, so that includes spaces. The * means zero or more times, so this regex means: || with any number of whitespace characters before or after it.
 
So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic