Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Java / Android equivalent to the iOS function "componentsJoinedByString" for an NSArray?

Is there anything like this in Java?

NSString *myString = [myArray componentsJoinedByString:@", "];

Here's an example of some output:

A, B, C, D

If not, what's the best way to accomplish this?

like image 977
Ethan Allen Avatar asked Nov 13 '12 21:11

Ethan Allen


1 Answers

Use TextUtils.join():

String myArray[] = new String[]{ "A", "B", "C", "D" };
String myString = TextUtils.join(", ", myArray);

There is also a version (same syntax) that takes an Iterable if you need to join something like an ArrayList.

like image 152
Cat Avatar answered Oct 06 '22 00:10

Cat