implode
implode(string $separator, array $array): string
Joins array elements into a single string with a delimiter.
C C23
C23
#include <stdio.h>
int main(void) {
const char *xs[] = {"a", "b", "c"};
for (int i = 0; i < 3; i++) {
printf(i ? "-%s" : "%s", xs[i]);
}
printf("\n");
return 0;
}
Output
a-b-c
C++ C++26
C++26
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::string> xs = {"a", "b", "c"};
for (size_t i = 0; i < xs.size(); ++i) {
if (i) std::cout << "-";
std::cout << xs[i];
}
std::cout << "\n";
return 0;
}
Output
a-b-c
C# v11.0.100
v11.0.100
var xs = new List<string> { "a", "b", "c" };
Console.WriteLine(string.Join("-", xs));
Output
a-b-c
Go v1.27.4
v1.27.4
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.Join([]string{"a", "b", "c"}, "-"))
}
Output
a-b-c
Java v27.0.1
v27.0.1
public class Main {
public static void main(String[] args) {
System.out.println(String.join("-", "a", "b", "c"));
}
}
Output
a-b-c
JavaScript v26.13.1
v26.13.1
console.log(["a", "b", "c"].join("-"))
Output
a-b-c
PHP v8.6.0
v8.6.0
echo implode("-", ["a", "b", "c"]);
Output
a-b-c
Python v3.15.2
v3.15.2
print("-".join(["a", "b", "c"]))
Output
a-b-c
Rust v1.99.1
v1.99.1
fn main() {
let xs = vec!["a", "b", "c"];
println!("{}", xs.join("-"));
}
Output
a-b-c
TypeScript v26.13.1
v26.13.1
const xs: string[] = ["a", "b", "c"];
console.log(xs.join("-"));
Output
a-b-c
Language differences
C: has no built-in containers or higher-order functions; the example uses fixed arrays/buffers and hand-written loops.
Go: strings.Join(arr, sep).
Java: String.join(sep, ...).
JavaScript: puts the array first as arr.join(sep).
PHP: implode(sep, arr).
Python: puts the delimiter first as sep.join(list).
Rust: joins string arrays with the join method.