是否可以告诉 String.split("(") 函数它必须仅按找到的第一个字符串 "("进行拆分?
例子:
String test = "A*B(A+B)+A*(A+B)";
test.split("(") should result to ["A*B" ,"A+B)+A*(A+B)"]
test.split(")") should result to ["A*B(A+B" ,"+A*(A+B)"]
最佳答案
是的,绝对的:
test.split("\\(", 2);
作为 the documentation for String.split(String,int)
解释:
The
limit
parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter.
关于java - 按 Java 中第一个找到的字符串拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9873674/