gpt4 book ai didi

java - 按 Java 中第一个找到的字符串拆分

转载 作者:IT老高 更新时间:2023-10-28 21:17:56 32 4
gpt4 key购买 nike

是否可以告诉 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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com