在 StringTokenizer 中有条件地添加字符串
Conditionally Adding String in StringTokenizer
我目前在使用 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="value from properties",属性中的值 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")
我尝试了您的代码并进行了这些更改,它工作正常。如果有什么不对请告诉我。
private String getPartialIssueList() {
String listFromProperties = getPropertyValue("propagateList");
StringTokenizer token = new StringTokenizer(listFromProperties, ",");
StringBuilder partialListString = new StringBuilder();
partialListString.append(" AND (key="); // updated with (
while (token.hasMoreTokens()) { // used tokens as it is a string
partialListString.append(token.nextToken() + " OR key=" + token.nextElement());
}
partialListString.append(")"); // added closed bracket
return partialListString.toString();
}
private String getPropertyValue(String propagateList) { // list is seperated by commas
return "'Ready for INT Deployment', 'Ready for UAT Deployment'";
}
以下代码给出了您预期的输出
private static String getPartialIssueList() {
String listFromProperties = "NN-09876, NN-65432, NN-65430";
StringTokenizer token = new StringTokenizer(listFromProperties, ",");
StringBuilder partialListString = new StringBuilder();
partialListString.append(" AND key=");
while (token.hasMoreTokens()) {
partialListString.append(token.nextToken());
if(token.hasMoreTokens()) {
partialListString.append(" OR key=" );
} else {
break;
}
}
return partialListString.toString();
}
在使用 hasMoreElements() 查看是否有下一个元素后,应调用方法 nextElement()。您的代码仅适用于偶数输入,因为每次检查后它都会调用 nextElement() 两次,因此如果检查后有两个元素,它会起作用,如果只有一个,则第一次调用会起作用,并且第二个抛出 NoSuchMethodException。
我目前在使用 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="value from properties",属性中的值 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")
我尝试了您的代码并进行了这些更改,它工作正常。如果有什么不对请告诉我。
private String getPartialIssueList() {
String listFromProperties = getPropertyValue("propagateList");
StringTokenizer token = new StringTokenizer(listFromProperties, ",");
StringBuilder partialListString = new StringBuilder();
partialListString.append(" AND (key="); // updated with (
while (token.hasMoreTokens()) { // used tokens as it is a string
partialListString.append(token.nextToken() + " OR key=" + token.nextElement());
}
partialListString.append(")"); // added closed bracket
return partialListString.toString();
}
private String getPropertyValue(String propagateList) { // list is seperated by commas
return "'Ready for INT Deployment', 'Ready for UAT Deployment'";
}
以下代码给出了您预期的输出
private static String getPartialIssueList() {
String listFromProperties = "NN-09876, NN-65432, NN-65430";
StringTokenizer token = new StringTokenizer(listFromProperties, ",");
StringBuilder partialListString = new StringBuilder();
partialListString.append(" AND key=");
while (token.hasMoreTokens()) {
partialListString.append(token.nextToken());
if(token.hasMoreTokens()) {
partialListString.append(" OR key=" );
} else {
break;
}
}
return partialListString.toString();
}
在使用 hasMoreElements() 查看是否有下一个元素后,应调用方法 nextElement()。您的代码仅适用于偶数输入,因为每次检查后它都会调用 nextElement() 两次,因此如果检查后有两个元素,它会起作用,如果只有一个,则第一次调用会起作用,并且第二个抛出 NoSuchMethodException。