split method with regex

J

josh

Hi I don't understand this output:

I have this string b = "boo:and:foo";

than I do:

String s[] = b.split("o");

than s [] has:

"b", "", ":and:f", "", ""

but why empty string??

Thanks
 
C

Chris Dollin

josh said:
Hi I don't understand this output:

I have this string b = "boo:and:foo";

than I do:

String s[] = b.split("o");

than s [] has:

"b", "", ":and:f", "", ""

but why empty string??

There's an empty string between the two `o`s of `boo`, ditto `foo`,
isn't there?
 
N

Nigel Wade

josh said:
josh said:
Hi I don't understand this output:
I have this string b = "boo:and:foo";
than I do:
String s[] = b.split("o");
than s [] has:
"b", "", ":and:f", "", ""
but why empty string??

There's an empty string between the two `o`s of `boo`, ditto `foo`,
isn't there?

sorry which string? I don't see it!

That's because it isn't there. How can you see it if it isn't there?

"o" is the delimiter. Your original string is:

b[delimiter][delimiter]:and:f[delimiter][delimiter]

remove the [delimiter]s and you are left with:

"b", "", ":and:f, "" and "".

The first string is what appears before the first delimiter. The second string
is what appears between the first and second delimiter i.e. nothing. The third
string is between the 2nd and 3rd delimiters. The 4th string is between the 3rd
and 4th delimiters, again nothing. The final string is what appears after the
final delimiter, nothing.

"I saw a string that wasn't there,
It wasn't there again today
O how I wish that string would go away"
 
P

Philipp Leitner

There's an empty string between the two `o`s of `boo`, ditto `foo`,
sorry which string? I don't see it!

:) Thats kind of the point of an empty string, isn't it?

Do understand whats happening just look at your String again:

"boo:and:foo"

Your separator is 'o', therefore everything between two 'o's becomes a
string in your result array. Considering this it is obvious that 'oo'
will always yield an empty string, since there is nothing (i.e. an
empty string) between these two 'o's ...
 
R

Roedy Green

Hi I don't understand this output:

I have this string b = "boo:and:foo";

than I do:

String s[] = b.split("o");

than s [] has:

"b", "", ":and:f", "", ""

but why empty string??

Thanks

replace all o's with commas. It should then appear logical.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top