gpt4 book ai didi

java - 在 StringTokenizer 中有条件地添加字符串

转载 作者:行者123 更新时间:2023-11-30 03:06:20 24 4
gpt4 key购买 nike

我目前在使用 StringTokenizer 时遇到了一个非常简单但令人困惑的问题。我想要实现的目标如下:

我的 URL 字符串为:

http://xxx:port/jira/rest/api/2/search?jql=project='TP' AND
(status='Ready for INT Deployment' or status = 'Ready for UAT Deployment')

以上是基本字符串。现在我想向上面的 URL 动态添加更多查询参数,如下所示:

"AND (key=NN-1234 OR key=NN-4321 OR key=NN-65343.....)"

这是我的代码:

private String getPartialIssueList() {
String listFromProperties = getPropertyValue("propagateList");
StringTokenizer token = new StringTokenizer(listFromProperties, ",");
StringBuilder partialListString = new StringBuilder();
partialListString.append(" AND key=");

while (token.hasMoreElements()) {
partialListString.append(token.nextElement() + " OR key=" + token.nextElement());
}
return partialListString.toString();
}

不知何故,这只在我有 2 个输入时才有效。我想要的是,在基本 url 之后应添加一个“AND”,后跟左括号“(”,然后是 key="属性中的值",属性中的值 2,属性中的值 3,然后是右大括号“)”。无论属性文件中有多少个 JIRA ID,它都应该继续添加“OR key=NN-5432 OR key="NN-9875”等。

这是我正在寻找的输出:

http://xxx:port/jira/rest/api/2/search?jql=project='TP' AND 
(status='Ready for INT Deployment' or status = 'Ready for UAT Deployment') AND
(key=NN-09876 OR key=NN-09873 OR key=NN-09376 OR key=NN-02876)

属性文件中的propagateList的当前值为

propagateList="NN-09876","NN-65432","NN-65430"

有人可以帮忙吗?

最佳答案

需要单独准备关键条件。

private String getPartialIssueList() {
String listFromProperties = getPropertyValue("propagateList");
StringTokenizer token = new StringTokenizer(listFromProperties, ",");
StringBuilder partialListString = new StringBuilder();
partialListString.append(" AND ");
partialListString.append("(");

// Prepare key condition
StringBuilder orKeys = new StringBuilder();
while(token.hasMoreElements()) {
// Replace token.nextToken() to token.nextToken().replaceAll("\"", "") if you don't want quotes
orKeys.append("key=" + token.nextToken() + " OR ");
}
// Remove last "OR" and concatenate
partialListString.append(orKeys.toString().replaceAll(" OR $", ""));
partialListString.append(")");

return partialListString.toString();
}

结果:

System.out.println(getPartialIssueList());
AND (key="NN-09876" OR key="NN-65432" OR key="NN-65430")

现在,将基本 URL 连接到动态查询:

String base = "http://xxx:port/jira/rest/api/2/search?jql=project='TP' AND (status='Ready for INT Deployment' or status = 'Ready for UAT Deployment')";
base += getPartialIssueList();
System.out.println(base);
------------------------------------------
http://xxx:port/jira/rest/api/2/search?jql=project='TP' AND
(status='Ready for INT Deployment' or status = 'Ready for UAT Deployment') AND
(key="NN-09876" OR key="NN-65432" OR key="NN-65430")

关于java - 在 StringTokenizer 中有条件地添加字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34691577/

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