explode
explode(string $separator, string $string, int $limit = PHP_INT_MAX): array
Splits a string into an array by a delimiter.
C C23
C23
#include <stdio.h>
int main(void) {
const char *s = "a,b,c";
printf("["); // hand-written split: scan and cut a segment at each delimiter
int first = 1;
for (const char *p = s; ; p++) {
if (*p == ',' || *p == '\0') {
printf(first ? "\"%.*s\"" : ", \"%.*s\"", (int)(p - s), s);
first = 0;
if (*p == '\0') break;
s = p + 1;
}
}
printf("]\n");
return 0;
}
Output
["a", "b", "c"]
C++ C++26
C++26
#include <iostream>
#include <string>
#include <vector>
int main() {
std::string s = "a,b,c";
std::vector<std::string> parts;
size_t pos = 0;
while (true) {
size_t comma = s.find(',', pos);
if (comma == std::string::npos) {
parts.push_back(s.substr(pos));
break;
}
parts.push_back(s.substr(pos, comma - pos));
pos = comma + 1;
}
std::cout << "[";
for (size_t i = 0; i < parts.size(); ++i) {
if (i) std::cout << ", ";
std::cout << '"' << parts[i] << '"';
}
std::cout << "]\n";
return 0;
}
Output
["a", "b", "c"]
C# v11.0.100
v11.0.100
var parts = "a,b,c".Split(',');
Console.WriteLine($"[{string.Join(", ", parts.Select(p => $"\"{p}\""))}]");
Output
["a", "b", "c"]
Go v1.27.4
v1.27.4
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.Split("a,b,c", ","))
}
Output
[a b c]
Java v27.0.1
v27.0.1
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
System.out.println(Arrays.toString("a,b,c".split(",")));
}
}
Output
[a, b, c]
JavaScript v26.13.1
v26.13.1
console.log("a,b,c".split(","))
Output
[ 'a', 'b', 'c' ]
PHP v8.6.0
v8.6.0
echo json_encode(explode(",", "a,b,c"));
Output
["a","b","c"]
Python v3.15.2
v3.15.2
print("a,b,c".split(","))
Output
['a', 'b', 'c']
Rust v1.99.1
v1.99.1
fn main() {
// split returns an iterator; collect it into a Vec
let parts: Vec<&str> = "a,b,c".split(',').collect();
println!("{:?}", parts);
}
Output
["a", "b", "c"]
TypeScript v26.13.1
v26.13.1
console.log("a,b,c".split(","));
Output
[ 'a', 'b', 'c' ]
Language differences
C: has no built-in containers or higher-order functions; the example splits by scanning for the separator with pointers, covering basic splitting — the limit variant is not implemented.
Go: strings.Split.
Java: String.split (note that Java split takes a regex and drops trailing empty strings).
JavaScript: String.split.
PHP: explode.
Python: str.split.
Rust: split returns an iterator; collect it into a Vec before use.