String method split(".") doesn't work

U

Ulf Nordlund

This works:

String s = "aaa:bbb";
String s0 = s.split(":")[0];

This don't (throws an ArrayOutOfBoundsException):

String s = "aaa.bbb";
String s0 = s.split(".")[0];

s.split() returns an empty array...
Any ideas why?
(I'm using j2sdk1.4.2_06)

Thanks,
/ulf
 
M

Martin Honnen

Ulf Nordlund wrote:

This don't (throws an ArrayOutOfBoundsException):

String s = "aaa.bbb";
String s0 = s.split(".")[0];

s.split() returns an empty array...
Any ideas why?

The argument to split is a regular expression pattern and in those
patterns the "." is a metacharacter so if you want to match it literally use
s.split("\\.")
 
T

Thomas Schodt

Ulf said:
String s = "aaa.bbb";
String s0 = s.split(".")[0];

You probably want

// "." is a wildcard, "\\." matches a literal "."
String s0 = s.split("\\.")[0];
 
S

Sudsy

Ulf said:
This works:

String s = "aaa:bbb";
String s0 = s.split(":")[0];

This don't (throws an ArrayOutOfBoundsException):

String s = "aaa.bbb";
String s0 = s.split(".")[0];

s.split() returns an empty array...
Any ideas why?

Because the argument to split is a regular expression? Try using the
string "\\." instead.
 
U

Ulf Nordlund

Martin Honnen skrev:
The argument to split is a regular expression pattern and in those
patterns the "." is a metacharacter so if you want to match it literally
use
s.split("\\.")

Of course! (I need to brush up on my regexp. Sorry for taking up your
time...)
Thanks!
/ulf
 
A

anonymous

Ulf said:
This works:

String s = "aaa:bbb";
String s0 = s.split(":")[0];

This don't (throws an ArrayOutOfBoundsException):

String s = "aaa.bbb";
String s0 = s.split(".")[0];

s.split() returns an empty array...
Any ideas why?
(I'm using j2sdk1.4.2_06)

Thanks,
/ulf

String s0 = s.split("[.]")[0];
 
T

Thomas G. Marshall

anonymous coughed up:
Ulf said:
This works:

String s = "aaa:bbb";
String s0 = s.split(":")[0];

This don't (throws an ArrayOutOfBoundsException):

String s = "aaa.bbb";
String s0 = s.split(".")[0];

s.split() returns an empty array...
Any ideas why?
(I'm using j2sdk1.4.2_06)

Thanks,
/ulf

String s0 = s.split("[.]")[0];



How many times does this need to be answered? Did any of you look through
at all to see if the answer was already (multiply) given?
 
A

anonymous

Thomas said:
anonymous coughed up:
Ulf said:
This works:

String s = "aaa:bbb";
String s0 = s.split(":")[0];

This don't (throws an ArrayOutOfBoundsException):

String s = "aaa.bbb";
String s0 = s.split(".")[0];

s.split() returns an empty array...
Any ideas why?
(I'm using j2sdk1.4.2_06)

Thanks,
/ulf

String s0 = s.split("[.]")[0];




How many times does this need to be answered? Did any of you look through
at all to see if the answer was already (multiply) given?
EXCUSE ME! When I first saw the original entry there were no answers.
Not all of us are complete morons.
 
S

Sudsy

Thomas G. Marshall wrote:
How many times does this need to be answered? Did any of you look through
at all to see if the answer was already (multiply) given?

I don't know which news server /you/ use but mine doesn't receive posts
instantaneously. Netnews is store-and-forward, no? Take a look at the
timestamps and you should notice that the original post and the replies
all occured in a short time period (relatively). There were certainly
no replies on my local news server at the moment I posted my reply.
Sorry, but that's just the way it works. So sue me for trying to offer
some assistance...
 
T

Thomas G. Marshall

Sudsy coughed up:
Thomas G. Marshall wrote:


I don't know which news server /you/ use but mine doesn't receive
posts instantaneously. Netnews is store-and-forward, no? Take a look
at the timestamps and you should notice that the original post and
the replies all occured in a short time period (relatively). There
were certainly no replies on my local news server at the moment I
posted my reply. Sorry, but that's just the way it works. So sue me
for trying to offer some assistance...


Mea culpa.

Understand though that it was not entirely a thoughtless reaction on my
part. I /did/ check the time stamps, and always do, but thought that in
this day and age that anything greater than about 20 minutes or so is
overkill. At least it's the way it seems from all the isp's I've used,
including those at work, and those used by my friends and their respective
work places. Actually, now even google (with their latest news) has more
than once shown my posts made from verizon in the time it takes for me to
switch screens, which floors me.

However I was wrong to use the 20 minute baseline criteria, and please
accept my apologies.
 
P

P.Hill

Thomas said:
Mea culpa.

Thanks for not getting defensive.

The difference in this case is probably very much related to original
message from Sweden, the first reply from Germany, the next from England
etc. ... the 1st 4 reps within 40 minutes, even one of which the OP
replied to within that first hour.

Your ISP at Verizon is apparently a lot fewer number of hops away from
Google than the OPs ISP in Sweden and the 1st few quick repliers.
However I was wrong to use the 20 minute baseline criteria, and please
accept my apologies.

A few years back I noticed Japanese posts often had double
replies by the time they showed up on my ISP in the USA, apparently
for the same interconnectivity store-and-forward reasons.

Well compared to sending a letter it's all instantaneous! :)

-Paul
 

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

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top