在 Groovy 中使用定界符拆分字符串并避免 IndexOutOfBoundsException
Splitting a string using a delimiter in Groovy and avoiding IndexOutOfBoundsException
我想将输入参数 inputDetails
拆分为单元级别。我正在使用 tokenize 来执行此操作。这是我的代码:
Groovy代码:
def inputDetails = "1234-a0-12;1111-b0-34";
def cDesc = inputDetails.tokenize(";");
for (int i=0; i<cDesc.size(); ++i)
{
def cVer = cDesc.get(i);
def cNum = cVer.tokenize("-");
def a = cNum.get(0);
def b = cNum.get(1);
def c = cNum.get(2);
println (" DEBUG : Input details are, ${a} : ${b} : ${c} \n");
}
输出:
DEBUG : Input details are, 1234 : a0 : 12
DEBUG : Input details are, 1111 : b0 : 34
此输出正确且符合预期。但是,如果我将 Groovy 代码的第一行更改为以下内容:
def inputDetails = "1234-a0-12;1111-b0";
我收到以下错误消息:
java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java_util_List$get.call(Unknown Source)
at Script1.run(Script1.groovy:9)
如何修复它以防止在同时支持 1234-a0-12;1111-b0-34
和 1234-a0-12;1111-b0
输入的同时获得 IndexOutOfBoundsException
?
您可以通过在“-”上进一步拆分将字符串拆分为二维列表:
def inputDetails = "1234-a0-12;1111-b0-34"
def elements = inputDetails.split(';').collect{it.split('-')}
elements
是 List<List<String>>
类型。打印时,它会产生:
[[1234, a0, 12], [1111, b0, 34]]
有了这个,您可以获得更大的灵活性,而不是硬编码数组索引。
与 "1234-a0-12;1111-b0"
一起拆分为 [[1234, a0, 12], [1111, b0]]
您可以使用 Groovy 的 multiple assignment 功能从第二次标记化中安全地获取 3 个值。考虑以下示例:
def inputDetails = "1234-a0-12;1111-b0-34"
def cDesc = inputDetails.tokenize(";")
cDesc.each { part ->
def (p1, p2, p3) = part.tokenize('-')
println "DEBUG: Input details are, ${p1} : ${p2} : ${p3}"
}
输出:
DEBUG: Input details are, 1234 : a0 : 12
DEBUG: Input details are, 1111 : b0 : 34
好处是这种方法可以防止 IndexOutOfBoundsException
或 NullPointerException
。如果我们将第一行更改为
def inputDetails = "1234-a0-12;1111-b0"
结果是:
DEBUG: Input details are, 1234 : a0 : 12
DEBUG: Input details are, 1111 : b0 : null
我想将输入参数 inputDetails
拆分为单元级别。我正在使用 tokenize 来执行此操作。这是我的代码:
Groovy代码:
def inputDetails = "1234-a0-12;1111-b0-34";
def cDesc = inputDetails.tokenize(";");
for (int i=0; i<cDesc.size(); ++i)
{
def cVer = cDesc.get(i);
def cNum = cVer.tokenize("-");
def a = cNum.get(0);
def b = cNum.get(1);
def c = cNum.get(2);
println (" DEBUG : Input details are, ${a} : ${b} : ${c} \n");
}
输出:
DEBUG : Input details are, 1234 : a0 : 12
DEBUG : Input details are, 1111 : b0 : 34
此输出正确且符合预期。但是,如果我将 Groovy 代码的第一行更改为以下内容:
def inputDetails = "1234-a0-12;1111-b0";
我收到以下错误消息:
java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java_util_List$get.call(Unknown Source)
at Script1.run(Script1.groovy:9)
如何修复它以防止在同时支持 1234-a0-12;1111-b0-34
和 1234-a0-12;1111-b0
输入的同时获得 IndexOutOfBoundsException
?
您可以通过在“-”上进一步拆分将字符串拆分为二维列表:
def inputDetails = "1234-a0-12;1111-b0-34"
def elements = inputDetails.split(';').collect{it.split('-')}
elements
是 List<List<String>>
类型。打印时,它会产生:
[[1234, a0, 12], [1111, b0, 34]]
有了这个,您可以获得更大的灵活性,而不是硬编码数组索引。
与 "1234-a0-12;1111-b0"
一起拆分为 [[1234, a0, 12], [1111, b0]]
您可以使用 Groovy 的 multiple assignment 功能从第二次标记化中安全地获取 3 个值。考虑以下示例:
def inputDetails = "1234-a0-12;1111-b0-34"
def cDesc = inputDetails.tokenize(";")
cDesc.each { part ->
def (p1, p2, p3) = part.tokenize('-')
println "DEBUG: Input details are, ${p1} : ${p2} : ${p3}"
}
输出:
DEBUG: Input details are, 1234 : a0 : 12
DEBUG: Input details are, 1111 : b0 : 34
好处是这种方法可以防止 IndexOutOfBoundsException
或 NullPointerException
。如果我们将第一行更改为
def inputDetails = "1234-a0-12;1111-b0"
结果是:
DEBUG: Input details are, 1234 : a0 : 12
DEBUG: Input details are, 1111 : b0 : null