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

Discussion in 'Java' started by Ulf Nordlund, Jan 8, 2005.

  1. Ulf Nordlund

    Ulf Nordlund Guest

    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
    Ulf Nordlund, Jan 8, 2005
    #1
    1. Advertising

  2. 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("\\.")

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jan 8, 2005
    #2
    1. Advertising

  3. Re: String method split(".") doesn't work - NOT

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


    You probably want

    // "." is a wildcard, "\\." matches a literal "."
    String s0 = s.split("\\.")[0];
    Thomas Schodt, Jan 8, 2005
    #3
  4. Ulf Nordlund

    Sudsy Guest

    Ulf Nordlund wrote:
    > 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.
    Sudsy, Jan 8, 2005
    #4
  5. Ulf Nordlund

    Ulf Nordlund Guest

    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
    Ulf Nordlund, Jan 8, 2005
    #5
  6. Ulf Nordlund

    anonymous Guest

    Ulf Nordlund wrote:
    > 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];
    anonymous, Jan 8, 2005
    #6
  7. anonymous coughed up:
    > Ulf Nordlund wrote:
    >> 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?




    --
    "His name was Robert Paulson. His name was Robert Paulson. His name was
    Robert Paulson..."
    Thomas G. Marshall, Jan 8, 2005
    #7
  8. Ulf Nordlund

    anonymous Guest

    Thomas G. Marshall wrote:
    > anonymous coughed up:
    >
    >>Ulf Nordlund wrote:
    >>
    >>>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.
    anonymous, Jan 8, 2005
    #8
  9. Ulf Nordlund

    Sudsy Guest

    Thomas G. Marshall wrote:
    <snip>
    > 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...
    Sudsy, Jan 8, 2005
    #9
  10. Sudsy coughed up:
    > Thomas G. Marshall wrote:
    > <snip>
    >> 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...



    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.


    --
    With knowledge comes sorrow.
    Thomas G. Marshall, Jan 9, 2005
    #10
  11. Ulf Nordlund

    P.Hill Guest

    Thomas G. Marshall wrote:
    >
    > 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
    P.Hill, Jan 11, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bruce Sam
    Replies:
    7
    Views:
    678
    John C. Bollinger
    Nov 9, 2004
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    688
    Alex Martelli
    Sep 17, 2004
  3. Sam Kong
    Replies:
    5
    Views:
    229
    Rick DeNatale
    Aug 12, 2006
  4. Jennifer Lee

    split doesn't work

    Jennifer Lee, Jun 14, 2010, in forum: Ruby
    Replies:
    4
    Views:
    105
    Jennifer Lee
    Jun 16, 2010
  5. Stanley Xu
    Replies:
    2
    Views:
    592
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page