1

I need to grab the whole arguments sequence as one single String, I'm using the following code, replacing commas and both square brackets.

Arrays.toString(args).replace(",", "").replace("[", "").replace("]", "");

Let's say I run it with the following arguments:

Hello, you're a programmer, you're a great guy

By using Arrays.toString() I'm getting:

[Hello, you're, a, programmer,, you're, a, great, guy]

So, by using the .replace() I actually get:

"Hello, you're a programmer, you're a great guy"

I just find the above code so messy, eventhough is working. How could I reduce such sentence in only one method?

2
  • what are the likely values of args? Commented Sep 3, 2013 at 23:06
  • 1
    The connection with CLASSPATH and RMI escapes me. Commented Sep 3, 2013 at 23:12

5 Answers 5

3

I like Commons Lang's StringUtils#join:

join(args);

, or, if you need a separator between the parts

join(args, ' ');

But I think you are asking the wrong question. If you want to preserve a String passed in from the command line exactly as is, you need to quote it in the shell. You will then get the whole thing as a single first String in the argument array. Otherwise, there is no way to recover for example consecutive spaces:

java -jar MyApp.jar "Hello, you're a programmer, you're a great guy"
Sign up to request clarification or add additional context in comments.

Comments

0

I would recommend using a StringBuilder. It's efficient, especially if you create the instance with the total length of your strings (check in a loop) as a constructor parameter.
For more info check: http://docs.oracle.com/javase/tutorial/java/data/buffers.html

Comments

0

Arrays of Strings can be converted to single String as given below.

    StringBuilder builder = new StringBuilder();
    for (String str: args)
    {
        builder.append(str);
    }
    System.out.println(builder.toString());

Comments

0

Are you for some reason splitting strings by their commas then putting them into an ArrayList? Any how, to answer the question specifically without trying to guess all the bad things going on...

You can probably do it taking out one replace but... meh.

public static void main(String[] args) {
        ArrayList<String> myArray = new new ArrayList<String>();
        myArray.add("hello");
        myArray.add("I");
        myArray.add("Am");
        myArray.add("Ben");
        myArray.add("!");
        System.out.println(myArray.toString().replaceAll("\\[(.*)\\]", "$1").replaceAll(",", ""));
    }

Comments

0

You could

  1. Get the person using the program to quote the string so that it is passed to your program as one argument
  2. Modify the way the command line works so that it doesn't interpret spaces as delimiters for arguments
  3. Modify main(), specifically how it takes arguments (I'm not sure this is possible or feasible)
  4. Write a new array subclass with a .toString() that behaves differently
  5. Write a new array subclass with a method that concatenates the array elements to a string
  6. Write a static method that concatenates array elements to a string and pass the argument array to it

The last is probably the least work, especially if you don't plan on using it much.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.