如何在java中使用char数组分隔符拆分字符串?

guh*_*hai 2 java split

像c#:

string[] Split(char[] separator, StringSplitOptions options)
Run Code Online (Sandbox Code Playgroud)

在java中有一个等价的方法吗?

Boh*_*ian 5

这样做你想要的:

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));
}
Run Code Online (Sandbox Code Playgroud)

输出(仅包括信息/兴趣的最终正则表达式):

(\,|\^|\*|\/|\+|\-|\&|\=|\<|\>|\=|\%|\(|\)|\{|\}|\;)
[foo, bar, hello, world]
Run Code Online (Sandbox Code Playgroud)