tags:

views:

154

answers:

7

if I have,

String[] s = new String[3];
s[0] = "Ap";
s[1] = "p";
s[2] = "le";

String result = ?

If I want to get Apple out of s without looping, how do I do that?

Any short cut?

A: 

If you know the length of your array, you can easily do the following:

String result = s[0] + s[1] +s[2];

Another option is to do the following, (which is purely academic, I would not use it in a real-world scenario as it would remove [, ], and <space> from your strings):

String result = Arrays.toString(s).replaceAll("[\\]\\[, ]", "");

Yet another option, to go along with the first attempt, but using a C-like formatter:

System.out.println(String.format("%s%s%s", s));
akf
If the length is 100? You need a loop.
Saj
A: 
String result = s[0] + s[1] + s[2];

If you have an unknown number of entries, I think you'll need a loop.

Carl Norum
I think he just gave an example. What if the array is passed in a method?
Saj
Well the array knows how big it is (in Java, anyway), and then you can loop. Otherwise, tough luck.
Carl Norum
+8  A: 

If the not looping is more important to you than preventing to import another library or if you are using apache commons lang already, anyway, you can use the StringUtils.join method

import org.apache.commons.lang.StringUtils;
String joined = StringUtils.join(s, "");

Maybe the Apache Commons have other methods that might be interesting for your project, as well. I found them to be a very useful resource for missing features in the native Java libraries.

Daff
Doesn't that just loop inside its implementation?
Carl Norum
+1 for not re-inventing the wheel.
SingleShot
Obviously there's going to be a loop _somewhere!_
John Kugelman
Yes it does check it out in the source http://kickjava.com/src/org/apache/commons/lang/StringUtils.java.htm . But mainly I just want to emphasize the idea of using libraries. If that's the only function you will use it doesn't make sense to import the whole jar of course.
Daff
I also checked the source and saw that it used StringBuffer. Using StringBuilder will make it faster. But that is good answer. It points that "if you need something check java library, if it is not in there it must be in apache commons" :)
JCasso
A: 

Java does not have a String.join() type method. You'll have to roll one yourself if you want to hide the loop.

John Kugelman
+2  A: 

Without looping, you can:

public String joinpart(String[] a, int i, String prefix) {
    if (i < a.length) {
        return joinpart(a, i + 1, prefix + a[i]);
    }
    return prefix;
}

then:

String[] a = new String[]{"Ap", "p", "le"};
String apple = joinpart(a, 0, "");

This is called a recursive solution.

Greg Hewgill
It's also quadratic in the size of the resulting string. You should have used `StringBuilder` in `joinpart` to construct the string.
jk
I don't think that's going to important for this particular learning exercise, so I didn't want to complicate the implementation. That would obscure the essence of the solution, which is what's important here.
Greg Hewgill
Good solution. What about using a string builder? We could specify the capacity and append in a for loop instead of using recursive. I ask this because I think it would be faster for bigger size arrays. Am i wrong?
JCasso
@jcasso: The question asked for a solution "without looping". I mentioned the use of StringBuilder in my comment above.
Greg Hewgill
Right i misread it.
JCasso
A: 

You have to use an external library or implement your own. But i don't recommend you to use a library only for this method. It will take longer to download and add a library instead of implementing your own.

You can use this function. And i don't think any external library would do a better one. Because the task is simple.

And there is no way to this without looping. Languages like C# which have built-in method for joining a string array also do this on background.

public static String arrayToString(String[] arr)
    {
        StringBuilder builder = new StringBuilder(arr.length);

        for (int i = 0; i < arr.length; i++)
            builder.append(arr[i]);

        return builder.toString();


    }
JCasso
A: 

using Dollar is simple as typing:

String[] array = new String[] { "Ap", "p", "le" };
String result = $(array).join(); // result now is "Apple"
dfa