aspose file tools*
The moose likes Java in General and the fly likes String split function and Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String split function and "||" String" Watch "String split function and "||" String" New topic
Author

String split function and "||" String

Khaled Mahmoud
Ranch Hand

Joined: Jul 15, 2006
Posts: 361
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


SCJP, SCJD,SCWCD,SCDJWS,SCEA 5 MCP-C#, MCP-ASP.NET - http://www.khaledinho.com/
Life is the biggest school
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3168
    
  10
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


Joanne
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String split function and "||" String
 
Similar Threads
Pop up calendar Problem
Google use Javascript or ajax on first page?
Need regex to solve
Why This Error??
New Mock Exam questions...