1

Possible Duplicate:
A method to reverse effect of java String.split()?

How do I take all the values within String[] args and make it into a single String variable?

Community
  • 1
  • 1
user372671
  • 35
  • 1
  • 2
  • 7
  • Can you show us what result that you want? – Crazenezz Apr 25 '12 at 02:52
  • Yes, it's definitely a duplicate. Can it be closed please? – Dawood ibn Kareem Apr 25 '12 at 03:14
  • There are *many* other Q/A's that deal with this; e.g. http://stackoverflow.com/questions/1978933/a-quick-and-easy-way-to-join-array-elements-with-a-separator-the-oposite-of-spl http://stackoverflow.com/questions/5283444/convert-array-of-strings-into-a-string-in-java and so on. – Stephen C Apr 25 '12 at 03:17

3 Answers3

3
  1. Iterate over the array
  2. concatenate the strings together

Note: it's more efficient to use a StringBuilder object, but this is a simpler example:

public String concat(String[] args) {
  String result = ""
  for (String arg : args) {
    result += arg;
  }
  return result;
}
Ben Taitelbaum
  • 7,343
  • 3
  • 25
  • 45
3

I prefer Apache Commons StringUtils. StringUtils.join(Object[] array, String separator) will join all the objects in the array with the given separator.

If you want a subarray of your current array (e.g. skip the first three elements), use this:

StringUtils.join(ArrayUtils.subarray(myArray, 3, myArray.length), ",");
Tim Pote
  • 27,191
  • 6
  • 63
  • 65
0

You can override toString() method and within this method, you can implement by doing loop through the String array and return the concatenated string in the format that you want.

Jasonw
  • 5,054
  • 7
  • 43
  • 48