2

like c#:

string[] Split(char[] separator, StringSplitOptions options)

is there an equivalent method in java?

7 Answers 7

5

This does what you want:

public static void main(String[] args) throws Exception
{
    char[] arrOperators = { ',', '^', '*', '/', '+', '-', '&', '=', '<', '>', '=', '%', '(', ')', '{', '}', ';' };
    String input = "foo^bar{hello}world"; // Expecting this to be split on the "special" chars
    String regex = "(" + new String(arrOperators).replaceAll("(.)", "\\\\$1|").replaceAll("\\|$", ")"); // escape every char with \ and turn into "OR"
    System.out.println(regex); // For interest only
    String[] parts = input.split(regex);
    System.out.println(Arrays.toString(parts));
}

Output (including the final regex for information/interest only):

(\,|\^|\*|\/|\+|\-|\&|\=|\<|\>|\=|\%|\(|\)|\{|\}|\;)
[foo, bar, hello, world]
Sign up to request clarification or add additional context in comments.

1 Comment

Great. It works. Thanks. This can be accepted as the standard answer.
1

Take a look at public String[] split(String regex) and java.util.regex.

Comments

1
String[] split(String)

You can turn a char[] into a String with String(char[]).

3 Comments

char[] arrOperators = { ',', '^', '*', '/', '+', '-', '&', '=', '<', '>', '=', '%', '(', ')', '{', '}', ';' };
I'm not entirely sure what you're looking for. The split method I supplied accepts regular expressions (regex). Here's a good tutorial on regex.
Illegal repetition near index 14
0

Possibly you want this. I'm not sure:

    String text = "abcdefg";
    System.out.println(Arrays.toString(text.split("d|f|b")));

Results in:

   [a, c, e, g]

Comments

0

Another level up in functionality is Guava's Splitter:

Splitter splitOnStuff = Splitter.on(CharMatcher.anyOf("|,&"))
                                .omitEmptyStrings();

Iterable<String> values = splitOnStuff.split("value1&value2|value3,value4");

Comments

0

You have to use Regex, to achieve this. It will tell this on which basis you have to separate. Like there is OR "|" operator. If you use

String regex = "a|b|c";
String[] tokens = str.split(regex)

It will split on a,b & c basis.

4 Comments

And how are you using regex ??? Read up regex yourself first before misleading others . What you have above is a string vairble called regex you could call it dog or cat as well
@Shahzeb, String.split(String) interprets its argument as a regex.
I want to split a String using
I want to split a String using: char[] arrOperators = { ',', '^', '*', '/', '+', '-', '&', '=', '<', '>', '=', '%', '(', ')', '{', '}', ';' };
0

Goold ol' StringTokenizer will do it too:

public static void main(String[] args) {
    String delim = ",^*/+-&=<>=%(){};";
    String str = "foo^bar{hello}world";
    StringTokenizer tok = new StringTokenizer(str, delim);
    while (tok.hasMoreTokens()) {
        System.out.println(tok.nextToken());
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.