Logo

Programming-Idioms

Build the list parts consisting of substrings of the input string s, separated by any of the characters ',' (comma), '-' (dash), '_' (underscore).
New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
#include <regex>
#include <string>
#include <vector>
using namespace std;
vector<string> parts;
regex r {R"([\Q,-_\E])"};
sregex_token_iterator i {
    s.begin(), s.end(), r, -1
}, n;
while (i not_eq n)
    parts.push_back(*i++);
using System.Text.RegularExpressions;
var parts = Regex.Split(s, "[,_-]");
var parts = s.Split(',', '-', '_');
var parts = s.split( RegExp(r"[,-_]") );
import "regexp"
re := regexp.MustCompile("[,\\-_]")
parts := re.Split(s, -1)
var parts = s.split(/[-_,]/)
import java.util.List;
import java.util.StringTokenizer;
import static java.util.Collections.list;
var x = new StringTokenizer(s, ",-_");
List<Object> parts = list(x);
import java.util.List;
import java.util.Scanner;
import static java.util.regex.Pattern.compile;
import static java.util.regex.Pattern.quote;
String p = '[' + quote(",-_") + ']';
Scanner x = new Scanner(s);
x.useDelimiter(compile(p));
List<String> parts = x.tokens().toList();
x.close();
import static java.util.regex.Pattern.quote;
String p = '[' + quote(",-_") + ']',
       parts[] = s.split(p, -1);
uses sysutils;
parts := s.split([',','_','-']);
my @parts = split(/[,\-_]/, $s);
import re
parts = re.split('[,_\-]', s)
from re import escape, split
parts = split(f'[{escape(',-_')}]', s)
parts = []
for i, x in enumerate(s):
    if x in ',-_':
        parts.append(s[a:i])
        a = i + 1
parts.append(s[a:])
parts = s.split( Regexp.union(",", "-", "_") )
let parts: Vec<_> = s.split(&[',', '-', '_'][..]).collect();