Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

String split() method bug?

Posted on 2004-04-05
9
Medium Priority
?
3,064 Views
Last Modified: 2007-12-19
Consider the following:

String s = "#1#sdsd###2#dsfdf##";
System.out.println(s);
String sa [] = s.split("#");
for (int i=0; i<sa.length; i++)
{
      System.out.println("token[" + i + "]: " + sa[i]);
}

It only returns 7 tokens instead of (the expected?) 8 tokens. Is this a bug or am I missing something?  I looked at java bug database but couldn't find anything.

Any thoughts?

  Thank you.
0
Comment
Question by:girionis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 13

Expert Comment

by:Webstorm
ID: 10757212
From API javadoc:
  " Trailing empty strings are therefore not included in the resulting array. "


0
 
LVL 13

Accepted Solution

by:
Webstorm earned 264 total points
ID: 10757215
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10757231
If you want the trailing empty strings :

String s = "#1#sdsd###2#dsfdf##";
System.out.println(s);
s=s+"end";
String sa [] = s.split("#");
for (int i=0; i<sa.length-1; i++)
{
     System.out.println("token[" + i + "]: " + sa[i]);
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 35

Author Comment

by:girionis
ID: 10757246
Maybe next time I should read the javadocs first.. thank you :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10757258
>> " Trailing empty strings are therefore not included in the resulting array. "

Do you want it to be?
0
 
LVL 2

Expert Comment

by:Andy_Neodynium
ID: 10757264
Trailing empty strings are not returned. Look up the docs for java.lang.String.split(). Those last two '#'s won't return anything

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

Expert Comment

by:CEHJ
ID: 10757287
If you DO want it to be, here's the correct RE

String sa[] = s.split("(?=#)");
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10757320
:-)
0
 
LVL 35

Author Comment

by:girionis
ID: 10757490
Thank you everyone for your replies.

>If you DO want it to be, here's the correct RE
>
>String sa[] = s.split("(?=#)");

No I don't, the real problem was an IndexOutOfBoundsException and I just solved by iterating at the length - 1 of the array.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question