0

I have arraylist "myorderdata". I want to retrieve this arraylist to one String variable and want "," separator between each value retrieve from array list .

I have tried to do this with String[] y = myorderdata.toArray(new String[0]); but this doesn't work . Can anyone help me ? Thanks in advance .

1
  • Have a look at this answer Commented Mar 28, 2012 at 11:46

3 Answers 3

4

Here's another variation to solve this task. Android offers two util methods to join the elements of an array or an Iterable in the class TextUtils.

// Join the elements of myorderdata and separate them with a comma:
String joinedString = TextUtils.join(",", myorderdata);
Sign up to request clarification or add additional context in comments.

Comments

1

Use this method to join arrays:

//for List of strings
public static String join(List<String> listStrings, String separator) {
  String[] sl = (String[]) listStrings.toArray(new String[]{});

  return join(sl, separator);
}


/**
 * Joins an array of string with the given separator
 * @param strings array of string
 * @param separator string to join with
 * @return a single joined string from array 
 */
public static String join(String[] strings, String separator) {
    StringBuffer sb = new StringBuffer();
    for (int i=0; i < strings.length; i++) {
        if (i != 0) sb.append(separator);
        sb.append(strings[i]);
    }
    return sb.toString();
}

2 Comments

first of all i want to retrive data from array list to string
still, you need to use the same method, but first you need to get arrays out from your list. for e.g.: foreach(String[] array : myorderdata){ String line = join(array, ","); }
0
StringBuffer sb = new StringBuffer();
    for( String string : myorderdata )
    {
        sb.append( string );
        sb.append( "," );           
    }
    sb.deleteCharAt( sb.length() );

    String result = sb.toString();

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.