Results 1 to 10 of 10
  1. #1
    tekberg is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default combine string[] into string like perl's join function

    I'm looking for a way to combine elements of a string array (or object.toString()s) into a single string. More specifically is there is a Java method or simple combination of methods, that can perform this kind of join in a single expression?

    I'm not looking for this:

    public static String join(String delimiter, Object[] array) {
    StringBuilder sb = new StringBuilder();
    boolean firstTime = true;
    for(Object element: array) {
    if (firstTime)
    firstTime = false
    else
    sb.append(delimiter);
    sb.append(element.toString());
    }
    return sb.toString();
    }

    I'm looking for something like this:

    String foo = array.join(",");

    I've looked in Java 6's Collections, Arrays and String classes with no luck.

    --tom

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    As far as I know there is no simple way to add element into a single string object. Easiest way to do this is, loop through the array and concat each element of the array in to previous one.

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default String array to string

    Maybe something like this:
    Java Code:
    String daString;
    for (int i = 0; i < daArray.length; i++)
        {
          daString += daArray[i];
         }
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    tekberg is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default

    The simplest example of what I'm looking for is this:

    XML Code:
         public static void main(String[] args) {
             final String[] array = new String[]{"one", "two", "three", "four"};
             String s = new Scanner(
                         new Readable() {
                             private int i = 0;
                             public int read(CharBuffer ch) {
                                 return (i < array.length)
                                             ? ch.append(array[i++]).length()
                                             : -1;
                             }
                         }
                 ).useDelimiter("\\Z").next();
             System.out.println(s);
         }
    The output is onetwothreefour.

    The problems are that the string array has to be final, and it is hardcoded in the read method.

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default uh, no...

    Not sure how you can get an output, because the above code doesn't compile.
    Java Code:
    String s = new Scanner(...
    Instantiating the Scanner class doesn't return a String. It's supposed to create an instance of the scanner class.

    I'm not sure what you want to do and I'm not sure what the problem is. Maybe you could try to explain again. I also don't see the relationship between the initial question and the posted code.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    tekberg is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default

    You have to look at the end of the expression to see how a String is generated. There are 3 expressions nested together. First a Scanner object is created. Second the 'useDelimiter' method sets the delimiter to EOF and returns the same Scanner object. Third the 'next' method scans the whole thing and returns a String.

  7. #7
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    but how is this simpler than CJSLMAN's example?
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Oh... OK... the way it's coded, I didn't catch that. For some crazy and unknown reason, the complier was giving me a couple of errors. I saved the program again and it worked (go figure). and yes I get your result as an output.

    Sorry, but I still don't understand what the problem is. You want the output to be "one two three four" ?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by CJSLMAN View Post
    Maybe something like this:
    Java Code:
    String daString;
    for (int i = 0; i < daArray.length; i++)
        {
          daString += daArray[i];
         }
    Luck,
    CJSL
    This is the way I suggest to workout. I don't think there is any other way than simpler this.

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Eranga... that's what I keep on thinking... unless the OP has some requirement that I'm not seeing....

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. how use string array while passing to a function
    By sks9s9 in forum New To Java
    Replies: 2
    Last Post: 02-03-2009, 12:44 PM
  2. Replies: 5
    Last Post: 01-11-2009, 10:12 AM
  3. String substring function
    By ravian in forum New To Java
    Replies: 6
    Last Post: 01-02-2008, 07:35 PM
  4. Replies: 0
    Last Post: 11-20-2007, 04:59 PM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •