Solved

String split() method bug?

Posted on 2004-04-05
9
3,056 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 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
Industry Leaders: 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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
ejb wildfly example 2 110
jboss 7.1 start up error 1 83
Java pass by reference 3 74
Which is best Image Resizing Web service 11 39
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

734 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