String split method

Discussion in 'Java' started by Anony!, Aug 2, 2004.

  1. Anony!

    Anony! Guest

    Hi

    Consider:

    String s = "abcdefghijklmnopqrstuwxyz";

    String[] result = s.split("[a-z]{3}"); //breaks string into substrings of
    length 3

    for (int i=0;i<result.length;i++)
    System.out.println(result);


    expect:
    abc
    def
    etc..

    but got nothing

    AAAAAA


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.732 / Virus Database: 486 - Release Date: 29/07/2004
     
    Anony!, Aug 2, 2004
    #1
    1. Advertising

  2. On Mon, 02 Aug 2004 05:47:26 +0000, Anony! wrote:

    > Consider:
    >
    > String s = "abcdefghijklmnopqrstuwxyz";
    >
    > String[] result = s.split("[a-z]{3}"); //breaks string into substrings of
    > length 3


    What you've just said is that [a-z][a-z][a-z] is the pattern that sits
    between each match.

    Was there something specifically wrong with using substring(start, end) ?

    --
    Some say the Wired doesn't have political borders like the real world,
    but there are far too many nonsense-spouting anarchists or idiots who
    think that pranks are a revolution.
     
    Owen Jacobson, Aug 2, 2004
    #2
    1. Advertising

  3. Anony!

    Anony! Guest


    > On Mon, 02 Aug 2004 05:47:26 +0000, Anony! wrote:
    >
    > > Consider:
    > >
    > > String s = "abcdefghijklmnopqrstuwxyz";
    > >
    > > String[] result = s.split("[a-z]{3}"); //breaks string into substrings

    of
    > > length 3

    >
    > What you've just said is that [a-z][a-z][a-z] is the pattern that sits
    > between each match.


    What should the delimiter be if I want it to break it into substrings of
    length 3. I thought the delimiter "[a-z]{3}" would be correct as it works
    properly when using Pattern and Match classes.

    > Was there something specifically wrong with using substring(start, end) ?


    no, just not as elegant as the string split function.




    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.732 / Virus Database: 486 - Release Date: 29/07/2004
     
    Anony!, Aug 2, 2004
    #3
  4. Anony!

    Liz Guest

    "Anony!" <> wrote in message
    news:H8lPc.28495$...
    >
    > > On Mon, 02 Aug 2004 05:47:26 +0000, Anony! wrote:
    > >
    > > > Consider:
    > > >
    > > > String s = "abcdefghijklmnopqrstuwxyz";
    > > >
    > > > String[] result = s.split("[a-z]{3}"); //breaks string into substrings

    > of
    > > > length 3


    Did you try escaping the curley brackets? "[a-z]\{3\}", or perhaps double
    slashes.


    > >
    > > What you've just said is that [a-z][a-z][a-z] is the pattern that sits
    > > between each match.

    >
    > What should the delimiter be if I want it to break it into substrings of
    > length 3. I thought the delimiter "[a-z]{3}" would be correct as it works
    > properly when using Pattern and Match classes.
    >
    > > Was there something specifically wrong with using substring(start, end)

    ?
    >
    > no, just not as elegant as the string split function.
    >
    >
    >
    >
    > ---
    > Outgoing mail is certified Virus Free.
    > Checked by AVG anti-virus system (http://www.grisoft.com).
    > Version: 6.0.732 / Virus Database: 486 - Release Date: 29/07/2004
    >
    >
     
    Liz, Aug 2, 2004
    #4
  5. On Mon, 02 Aug 2004 06:26:47 +0000, Anony! wrote:

    >
    >> On Mon, 02 Aug 2004 05:47:26 +0000, Anony! wrote:
    >>
    >> > Consider:
    >> >
    >> > String s = "abcdefghijklmnopqrstuwxyz";
    >> >
    >> > String[] result = s.split("[a-z]{3}"); //breaks string into substrings

    > of
    >> > length 3

    >>
    >> What you've just said is that [a-z][a-z][a-z] is the pattern that sits
    >> between each match.

    >
    > What should the delimiter be if I want it to break it into substrings of
    > length 3. I thought the delimiter "[a-z]{3}" would be correct as it works
    > properly when using Pattern and Match classes.


    It shouldn't. Split *does not* do what you want. In all cases,
    split(regex) consumes the text matching the regex. No matter what
    delimiter pattern you choose some characters will be lost.

    >> Was there something specifically wrong with using substring(start, end)
    >> ?

    >
    > no, just not as elegant as the string split function.


    /** Totally untested. */
    private static String[] fixedSegments (String s, int length) {
    assert (length > 0);

    int segments = (s.length() + length - 1) / length;
    if (s.length () % length != 0)
    ++segments; // hold the last partial segment

    String[] r = new String[segments];
    for (int i = 0; i < segments; ++i)
    r = s.substring (i * length,
    Math.min ((i + 1) * length, s.length ()));

    return r;
    }

    --
    Some say the Wired doesn't have political borders like the real world,
    but there are far too many nonsense-spouting anarchists or idiots who
    think that pranks are a revolution.
     
    Owen Jacobson, Aug 2, 2004
    #5
  6. Anony!

    andrewh1 Guest

    Anony! wrote:

    > Hi
    >
    > Consider:
    >
    > String s = "abcdefghijklmnopqrstuwxyz";
    >
    > String[] result = s.split("[a-z]{3}"); //breaks string into substrings of
    > length 3


    No it doesn't. You need to read the docs.

    The docs for 1.4

    ***********************************************
    split

    public String[] split(String regex)

    Splits this string around matches of the given regular expression.

    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.

    The string "boo:and:foo", for example, yields the following results
    with these expressions:

    Regex Result
    : { "boo", "and", "foo" }
    o { "b", "", ":and:f" }

    Parameters:
    regex - the delimiting regular expression
    Returns:
    the array of strings computed by splitting this string around
    matches of the given regular expression
    ********************************************************************
    Effectively you are getting it to give you the strings separated by (and
    not including) any lower case letter in the alphabet. In your case lots
    of "".
    However since it 'doesn't include trailing empty strings' it should
    return an empty array.

    I may be wrong, but I doubt using regular expressions is the way to go
    if you simply want a string chopped into equal sized fragments
    disregarding character content. String.Substring() method is probably
    the best.

    Seems to me that regular expressions are more for dealing with strings
    where string content (ie the character sequence) defines the operations.

    Cheers

    Andrew

    >
    > for (int i=0;i<result.length;i++)
    > System.out.println(result);
    >
    >
    > expect:
    > abc
    > def
    > etc..
    >
    > but got nothing
    >
    > AAAAAA
    >
    >
    > ---
    > Outgoing mail is certified Virus Free.
    > Checked by AVG anti-virus system (http://www.grisoft.com).
    > Version: 6.0.732 / Virus Database: 486 - Release Date: 29/07/2004
    >
    >
     
    andrewh1, Aug 2, 2004
    #6
  7. Anony!

    Chris Uppal Guest

    Anony! wrote:

    > > Was there something specifically wrong with using substring(start, end)
    > > ?

    >
    > no, just not as elegant as the string split function.


    Elegance is in the eye of the beholder. To /my/ eye, using regexps when no
    regexp is needed looks like unecessary bloat, obfuscation, and the misuse of
    heavyweight (and slow[*]) features to perform lightweight operations.

    (Not to say that it is /necessarily/ wrong, though, e.g. if I had lots of
    string splitting to do, nearly all of which needed full regexp power, then I
    would quite probably use it for the simple cases too. Just to keep things
    simple and regular.)

    [*] I admit I haven't measured the difference.

    -- chris
     
    Chris Uppal, Aug 2, 2004
    #7
    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. Replies:
    2
    Views:
    472
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    708
    Alex Martelli
    Sep 17, 2004
  3. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    220
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    248
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    623
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page