View Problem

Join the elements of a list, separated by commas

Given the list [Apple, Banana, Carrot] produce "Apple, Banana, Carrot"
ExpandDiskEdit
ruby
string = fruit.join(', ')
ExpandDiskEdit
java
StringBuffer sb = new StringBuffer();
for (Iterator it = fruit.iterator(); it.hasNext();) {
sb.append(it.next());
if (it.hasNext()) {
sb.append(", ");
}
}
String result = sb.toString();
ExpandDiskEdit
java 1.5 or later
StringBuilder sb = new StringBuilder(fruit.get(0));
for (String item : fruit.subList(1, fruit.size())) sb.append(", ").append(item);
String result = sb.toString();
ExpandDiskEdit
java org.apache.commons
String result = StringUtils.join(fruit, ", ");
DiskEdit
perl
print join ', ', qw(Apple Banana Carrot);
DiskEdit
perl
# Longer and less efficient than join(), but illustrates
# Perl's foreach operator, which can be useful for
# less trivial problems with lists

@list = ('Apple', 'Banana', 'Carrot');
foreach $fruit (@list) {
print "$fruit,";
}
print "\n";
DiskEdit
perl
my @a = qw/Apple Banana Carrot/;
{
local $, = ", ";
print @a
}
print "\n";
DiskEdit
perl
my @a = qw/Apple Banana Carrot/;
{
local $" = ", ";
print "@a\n";
}
ExpandDiskEdit
groovy
string = fruit.join(', ')
ExpandDiskEdit
groovy
string = fruit.toString()[1..-2]
ExpandDiskEdit
scala
val result =
((fruit.tail foldLeft (new StringBuilder(fruit.head))) {(acc, e) => acc.append(", ").append(e)}).toString
ExpandDiskEdit
scala
val result = fruit.mkString(",")
ExpandDiskEdit
scala 2.9.0.1
val fruit = List[String]("Apple", "Banana", "Carrot")
println(fruit.mkString(", "))
DiskEdit
python
print ", ".join(['Apple', 'Banana', 'Carrot'])
ExpandDiskEdit
cpp C++/CLI .NET 2.0
String^ result = String::Join(L", ", fruit->ToArray());
ExpandDiskEdit
cpp boost
string fruits[] = {"Apple", "Banana", "Carrot"};
string result = boost::algorithm::join(fruits, ", ");
ExpandDiskEdit
fsharp
let result = String.Join(", ", [|"Apple"; "Banana"; "Carrot"|])
ExpandDiskEdit
fsharp
let result = (List.fold_left (fun acc item -> acc ^ (", " ^ item)) (List.hd fruit) (List.tl fruit))
ExpandDiskEdit
fsharp
let result = (List.fold_left (fun (acc : StringBuilder) (item : string) -> acc.Append(", ").Append(item)) (new StringBuilder(List.hd fruit)) (List.tl fruit)).ToString()
ExpandDiskEdit
erlang
Result = string:join(Fruit, ", "),
ExpandDiskEdit
erlang
Result = lists:foldl(fun (E, Acc) -> Acc ++ ", " ++ E end, hd(Fruit), tl(Fruit)),
ExpandDiskEdit
erlang
Result = lists:flatten([ hd(Fruit) | [ ", " ++ X || X <- tl(Fruit)]]).
DiskEdit
ocaml
let () =
let lst = ["Apple"; "Banana"; "Carrot"] in
let str = String.concat ", " lst in
print_endline str
DiskEdit
csharp .NET 3.5
using System.Collections.Generic;
public class JoinEach {
public static void Main() {
var list = new List<string>() {"Apple", "Banana", "Carrot"};
System.Console.WriteLine( string.Join(", ", list.ToArray()) );
}
}
ExpandDiskEdit
php
$string = implode(", ", $fruits);
DiskEdit
haskell
import Data.List

let join = intercalate ", " ["Apple", "Banana", "Carrot"]
DiskEdit
clojure
(apply str (interpose ", " '("Apple" "Banana" "Carrot")))
ExpandDiskEdit
fantom
["Apple", "Banana", "Carrot"].join(", ")
ExpandDiskEdit
go
s := strings.Join([]string {"Apple", "Banana", "Carrot"}, ", ")

Submit a new solution for ruby, java, perl, groovy ...