Solved

String split() method bug?

Posted on 2004-04-05
9
3,054 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
  • 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 66 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 236
Java string replace 11 61
JavaFX TableView not displaying correctly 3 101
eclipse console opening separately 2 35
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

680 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