views:

114

answers:

5
Map<String, String[]> map = request.getParameterMap();
for (Entry<String, String[]> entry : map.entrySet())
{
    String name = entry.getKey();
    String[] values = entry.getValue();
    String valuesStr = Arrays.toString(values).trim();
    LOGGER.warn(valuesStr);

I'm trying to look at a request parameter value using the code above.

Why does Arrays.toString(values).trim(); bracket the parameter value so it looks like this:

[Georgio]

What's the best way to get the String here without the brackets?

If I do this:

String valuesStr = values[0].trim();

it seems there is a risk of losing subsequent values in the array.

+1  A: 

Java's default implementation of the Arrays toString method is like that. You can create a class that extends it, specialized for what you want, and overwrite the toString method to make it generate a string of your liking, without the "[" "]"s, and with other restrictions of your liking and need.

Luis Miguel
Technically you cannot extend `java.util.Arrays`, since it does not have a public (or protected) constructor.
Matt Solnit
And furthermore, it would have no meaning to do so since these methods are static.
Kirk Woll
You are very right. My apologies. Even from taking a second look at the code, it comes obvious that it is static, given the way the toString method is called. He should then create another static method in any class, to deal with the Strings vector in a customized way.
Luis Miguel
+1  A: 

Hi faq. I believe this is just how the implementation of Arrays.toString(Object[]) works, at least on the Sun JVM. If the array had multiple elements, you would see something like [foo, bar, baz].

Are you looking to basically get the same output, without the brackets? E.g. foo, bar, baz ? If so, then it should be pretty easy to write your own method.

Matt Solnit
+4  A: 

If your desired output is a list of values separated by commas (or something else), I like the approach with Guava's Joiner:

String valuesStr = Joiner.on(",").join(values)

smmv
+6  A: 

That is just the default formatting applied by the Arrays.toString(Object[]) method. If you want to skip the brackets you can build the string yourself, for example:

public static String toString(Object[] values)
{
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < values.length; i++)
    {
        if (i != 0)
            sb.append(", ");
        sb.append(values[i].toString());
    }
    return sb.toString();
}
Grodriguez
You should use `StringBuilder` (not synchronized) instead of `StringBuffer` (synchronized).
Steve Kuo
@Steve: Good point, thanks. I've updated my answer.
Grodriguez
I think `sb.append(', ');` needs double-quote characters instead.
faq
@faq: Indeed :). Thanks for pointing it out.
Grodriguez
+1  A: 

I would suggest you use a string-builder or guava's joiner but if you want a quick fix,you can try this:

Arrays.toString(values).split("[\\[\\]]")[1];

Note: Use the above method only if the values themselves doesn't contain bracket's in them.

StringBuilder Implementaion:

 static String toString(Object ... strings)
 {
    if(strings.length==0)
        return "";
    StringBuilder sb=new StringBuilder();
    int last=strings.length-1;
    for(int i=0;i<last;i++)
        sb.append(strings[i]).append(",");
    sb.append(strings[last]);
    return sb.toString();
 }

UPDATE:

Using substring:

String s=(s=Arrays.toString(arr)).substring(1,s.length()-1); 
Emil
And what if the values themselves contain brackets?
Roland Illig
Your sample code using `StringBuilder` will break if `objs.length==0`. See my answer to this same question for a solution that handles this case properly.
Grodriguez
@Downvoter:please explain.
Emil
@Emil: Is there any reason for the second `toString` call in `Arrays.toString(values).toString().split(...)`?
Grodriguez
@Grodriguez: No,it was my mistake.Corrected it.
Emil