How to join a range of array string slots with blank delimiters? Almost opposite of string.split()?

Discussion in 'Java' started by Gunter Hansen, Aug 25, 2011.

  1. Assume I split a text line with a command like:

    String[] part = line.split("\\s+");

    Ok, fine, but how is the opposite command?
    Keep in mind that the delimiter between the joined slots of the target string should be one blank.

    Moreover: Can I join e.g. only slots [3],...,[MAX]?

    So I need a command similar to:

    String joined = part[3,MAX].join(' ');

    How ca I achieve this?

    If this is not possible directly then maxbe with a work around?

    Gunther
     
    Gunter Hansen, Aug 25, 2011
    #1
    1. Advertising

  2. Gunter Hansen

    Yuxuan Wang Guest

    回å¤ï¼šRe: How to join a range of array string slots with blank delimiters? Almost opposite of string.split()?

    Peter's right. Apache StringUtils.join()
     
    Yuxuan Wang, Aug 25, 2011
    #2
    1. Advertising

  3. Gunter Hansen

    Roedy Green Guest

    On 25 Aug 2011 05:27:12 GMT, (Gunter Hansen)
    wrote, quoted or indirectly quoted someone who said :

    >
    >If this is not possible directly then maxbe with a work around?


    I would do it like this:

    final FastCat sb = new FastCat ( slots.length * 2 - 1 );
    for ( String slot ; slots )
    {
    if( sb.length() != 0 )
    {
    sb.append( ' ' );
    }
    sb.append( slot );
    }
    final String joined = sb.toString();

    see http://mindprod.com/products1.html#FASTCAT

    Alternatively you could compute the final string length and allocate a
    char[]. Then plop the pieces in, and convert to a string, which is
    roughly what FastCat does under the hood.

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
    the search for a superior moral justification for selfishness.
    ~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
     
    Roedy Green, Aug 25, 2011
    #3
  4. Gunter Hansen

    Arne Vajhøj Guest

    Re: How to join a range of array string slots with blank delimiters?Almost opposite of string.split()?

    On 8/25/2011 1:27 AM, Gunter Hansen wrote:
    > Assume I split a text line with a command like:
    >
    > String[] part = line.split("\\s+");
    >
    > Ok, fine, but how is the opposite command?
    > Keep in mind that the delimiter between the joined slots of the target string should be one blank.


    You can always use a StringBuilder and a loop.

    If you like weird one liners try:

    line = Arrays.toString(part).replaceAll("[\\[\\],]", "");

    > Moreover: Can I join e.g. only slots [3],...,[MAX]?
    >
    > So I need a command similar to:
    >
    > String joined = part[3,MAX].join(' ');
    >
    > How ca I achieve this?


    StringBuilder and a loop.

    Arne
     
    Arne Vajhøj, Aug 26, 2011
    #4
  5. Re: How to join a range of array string slots with blank delimiters?Almost opposite of string.split()?

    On 8/25/2011 1:49 AM, Peter Duniho wrote:
    > On 8/24/11 10:27 PM, Gunter Hansen wrote:
    >> Assume I split a text line with a command like:
    >>
    >> String[] part = line.split("\\s+");
    >>
    >> Ok, fine, but how is the opposite command?
    >> Keep in mind that the delimiter between the joined slots of the target
    >> string should be one blank.
    >>
    >> Moreover: Can I join e.g. only slots [3],...,[MAX]?
    >>
    >> So I need a command similar to:
    >>
    >> String joined = part[3,MAX].join(' ');
    >>
    >> How ca I achieve this?
    >>
    >> If this is not possible directly then maxbe with a work around?

    >
    > I am surprised the feature doesn't exist in the JDK; in .NET there's a
    > String.Join() method that does exactly what you're asking about. But
    > AFAIK, it doesn't.


    Even split was just added in 1.4 - before that it was StringTokenizer.

    > And I have to admit, I think Java's take on the "split" function is
    > pretty cool, what with regex support and all (which .NET doesn't
    > provide, not as a String.Split() feature anyway).


    ..NET has Regex.Split.

    Arne
     
    Arne Vajhøj, Aug 26, 2011
    #5
  6. Gunter Hansen

    Roedy Green Guest

    On Thu, 25 Aug 2011 13:05:22 -0700, Roedy Green
    <> wrote, quoted or indirectly quoted
    someone who said :

    >for ( String slot ; slots )


    oops that should read

    for ( String slot : slots )
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
    the search for a superior moral justification for selfishness.
    ~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
     
    Roedy Green, Sep 1, 2011
    #6
    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. tin gherdanarra

    slots? SLOTS?

    tin gherdanarra, Oct 12, 2005, in forum: Python
    Replies:
    2
    Views:
    2,324
    Peter Hansen
    Oct 13, 2005
  2. dmitrey
    Replies:
    4
    Views:
    465
  3. eliascm
    Replies:
    0
    Views:
    745
    eliascm
    Feb 14, 2011
  4. Albert Schlef

    String#split and capturing delimiters

    Albert Schlef, Oct 30, 2009, in forum: Ruby
    Replies:
    3
    Views:
    193
    Rajinder Yadav
    Oct 30, 2009
  5. Replies:
    11
    Views:
    584
    James Taylor
    Jul 29, 2005
Loading...

Share This Page