0

What I want to do is take an input_string, as well as a char[] of delimiters and then split that input_string using all of the delimiters in that char[].

This is the code I have so far, but I have not found a way to utilize the char[] in the split function. If this isn't possible then I realize that I could have someone create a regex for a delimiter, but this is meant to be as simple as possible to change anything, so adding an extra delimiter would be as simple as adding it to the delimiters array.

public String[] stringSplit(char[] delimiters, String input_words) {
    String[] input_words_split = null;
    input_words_split = input_words.split(delimiters[0]);

    return input_words_split;

}
terrabl
  • 743
  • 3
  • 8
  • 23
  • 3
    And what is the question? – Grzegorz Górkiewicz Feb 15 '17 at 00:34
  • What is the question? do you not want to iterate through the entire array? – persistence.xml Feb 15 '17 at 00:34
  • "Could have someone create" - sorry, no. Regexps aren't that hard. – Thorbjørn Ravn Andersen Feb 15 '17 at 00:36
  • You could read the array and create a regular expression to pass to `String#split()` from it, making sure to escape any characters that need to be escaped. – David Choweller Feb 15 '17 at 00:38
  • 2
    `String splitRegex = Arrays.stream(new String(delimiters).split("")).map(Pattern::quote).collect(Collectors.joining("|")); return input_words.split(splitRegex);` – shmosel Feb 15 '17 at 00:39
  • 1
    @shmosel, that is excellent. – David Choweller Feb 15 '17 at 00:45
  • 1
    Much simpler to just do this: `input_words_split = input_words.split("[" + String.valueOf(delimiters) + "]");` – neildo Feb 15 '17 at 00:46
  • 1
    @neildo That will fail for special characters. – shmosel Feb 15 '17 at 00:47
  • 1
    @shmosel true, if you need to use `]` or `[` or \ as delimiters. Otherwise, should be fine. – neildo Feb 15 '17 at 00:51
  • Thank you for being helpful, @shmosel, I've never really had experience with regexs and their syntax confuses me no matter how much I look at it. I have a couple of questions for you. When I print the `splitRegex` after I create it using the magic line of code above with the delimeters `{' ', 'a'}` it gives me the splitRegex = `\Q \E|\Qa\E`. Could you explain what the \Q and the \E mean? – terrabl Feb 15 '17 at 00:53
  • 1
    @neildo I did a little digging, and it looks like your way will work if you enclose the delimiter block in `Pattern.quote()`: `return input_words.split("[" + Pattern.quote(String.valueOf(delimiters)) + "]");` – shmosel Feb 15 '17 at 00:53
  • @shmosel, Boom! Even better. – neildo Feb 15 '17 at 00:55
  • @terrabl It's a form of escaping. It tells the regex engine not treat them as special characters. See [here](http://stackoverflow.com/q/15409296/1553851) for more info. – shmosel Feb 15 '17 at 00:58

1 Answers1

2

In a recursive way :

 private static void split(String input, String[] separators, int index, List<String> output) {
        if(index == separators.length) {

            output.add(input);

        } else {
            final String separator = separators[index];

            for (String s : input.split(separator)) {
                split(s, separators, index + 1, output);
            }

        }
    }

How to use :

public static void main(String[] args) {

        String input = "te,st;rr;a,e";
        String[] separators = {",", ";"};

        List<String> output = new ArrayList<>();

        split(input, separators, 0, output);

        System.out.println(output);
    }

Output :

[te, st, rr, a, e]
Mouad EL Fakir
  • 3,609
  • 2
  • 23
  • 37