像c#:
string[] Split(char[] separator, StringSplitOptions options)
Run Code Online (Sandbox Code Playgroud)
在java中有一个等价的方法吗?
这样做你想要的:
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)
| 归档时间: |
|
| 查看次数: |
5652 次 |
| 最近记录: |