【发布时间】:2014-09-23 16:55:23
【问题描述】:
我有一个 Groovy 方法,目前有效,但 真实 丑陋/骇人听闻:
def parseId(String str) {
System.out.println("str: " + str)
int index = href.indexOf("repositoryId")
System.out.println("index: " + index)
int repoIndex = index + 13
System.out.println("repoIndex" + repoIndex)
String repoId = href.substring(repoIndex)
System.out.println("repoId is: " + repoId)
}
运行时,您可能会得到如下输出:
str: wsodk3oke30d30kdl4kof94j93jr94f3kd03k043k?planKey=si23j383&repositoryId=31850514
index: 59
repoIndex: 72
repoId is: 31850514
如您所见,我只是对从字符串中获取repositoryId 值(= 运算符之后的所有内容)感兴趣。有没有更有效/更规范的方式来做到这一点或这是唯一的方式?
【问题讨论】:
-
使用正则表达式捕获您要查找的模式
标签: string syntax groovy substring