2

I have String ArrayList has some elements but I need to convert it to string each element is separated from other by - this is the arrayList

List<String> items = new ArrayList<String>();
items.add("a");
items.add("b");
items.add("c");
:
:

I need to get the string like this

"a-b-c-..."

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Naham Soft
  • 91
  • 2
  • 12
  • 1
    Create a `StringBuilder` object and use `append` to attached each string, then convert the StringBuilder to a String. Use `StringBuffer` instead of `StringBuilder` if code is synchronized. – Al Lelopath May 06 '16 at 15:35
  • Oh good. but there is no method like implode in php – Naham Soft May 06 '16 at 15:44
  • Are you writing this in php or java? – Al Lelopath May 06 '16 at 15:46
  • sure on java . but I have told you about php this problem sove by implode("-",array); and I am looking for like this in java please see the accepted answer. any way I appreciated your help – Naham Soft May 06 '16 at 15:56

7 Answers7

18

If you are using Java 8 you can use the join function of the String class:

String result = String.join("-", items);

If you are programming for Android, Java 8 is not available. You can instead use:

String result = TextUtils.join("-", items);
Shadow
  • 3,926
  • 5
  • 20
  • 41
5

One line:

String allItems = String.join("-", items);

Documentation is here.

VGR
  • 40,506
  • 4
  • 48
  • 63
2

You can use Joiner from google-guava:

List<String> items = new ArrayList<String>();
items.add("a");
items.add("b");
items.add("c");

String result = Joiner.on("-").join(items);

here is the link - https://github.com/google/guava/wiki/StringsExplained#joiner

Oleg Khalidov
  • 5,108
  • 1
  • 28
  • 29
2

No guava libs will be possible by manipulating the toString result

public static void main(String[] args) {
        List<String> items = new ArrayList<String>();
        items.add("a");
        items.add("b");
        items.add("c");
        System.out.println(items.toString().replace(", ", "-"));
        // no [ ] 
        System.out.println(items.toString().substring(1, items.toString().length() - 1).replace(", ", "-"));
    }
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1
    List<String> items = new ArrayList<String>();
    items.add("a");
    items.add("b");
    items.add("c");
    StringBuilder builder = new StringBuilder();
    builder.append(items.get(0));
    for (int i = 1; i < items.size(); i++) {
        builder.append("-").append(items.get(i));
    }
    String result = builder.toString();
    System.out.println(""+ result);
Masum
  • 4,879
  • 2
  • 23
  • 28
0
  StringBuilder builder = new StringBuilder();
    builder.append(items.get(0));
    for (int i = 1; i < items.size(); i++) {
        builder.append("-").append(items.get(i));
    }
    String result = builder.toString();
Lina Shyshova
  • 546
  • 5
  • 12
0

You can create a simple function for this

List<String> items = new ArrayList<String>();
items.add("a");
items.add("b");
items.add("c");

Log.d("myFormattedList",getFormattedString(items));

private String getFormattedString() {
    String tempString = "";
    for (String temp: items) {
        tempString += temp;
    }
    tempString.substring(0,str.length()-1); //remove last char "-"
    return tempString;
}
Emin Ayar
  • 1,104
  • 9
  • 13