下面的代码有什么问题,比较groovy中的2个字符串
我正在尝试在一行中对两个参数进行比较,使其看起来更整洁
values()
抛出异常 -
<p>Sheet1</p>
<table border="1" style="border-collapse: collapse; width: 100%;">
<tbody>
<tr>
<td style="width: 10%; text-align: center;">A</td>
<td style="width: 10%; text-align: center;">B</td>
<td style="width: 10%; text-align: center;">C</td>
<td style="width: 10%; text-align: center;">D</td>
<td style="width: 10%; text-align: center;">E</td>
<td style="width: 10%; text-align: center;">F</td>
<td style="width: 10%; text-align: center;">G</td>
<td style="width: 10%; text-align: center;">H</td>
<td style="width: 10%; text-align: center;">I</td>
<td style="width: 10%; text-align: center;">J</td>
</tr>
<tr>
<td style="width: 10%; text-align: center;">Category</td>
<td style="width: 10%; text-align: center;">Item Name</td>
<td style="width: 10%; text-align: center;">Material 1</td>
<td style="width: 10%; text-align: center;">Material 2</td>
<td style="width: 10%; text-align: center;">Material 3</td>
<td style="width: 10%; text-align: center;">Material 4</td>
<td style="width: 10%; text-align: center;">Material 5</td>
<td style="width: 10%; text-align: center;">Material 6</td>
<td style="width: 10%; text-align: center;">Material 7</td>
<td style="width: 10%; text-align: center;">Material 8</td>
</tr>
<tr>
<td style="width: 10%; text-align: right;">Group A</td>
<td style="width: 10%; text-align: right;">A</td>
<td style="width: 10%; text-align: right;"></td>
<td style="width: 10%; text-align: right;"></td>
<td style="width: 10%; text-align: right;"></td>
<td style="width: 10%; text-align: right;"></td>
<td style="width: 10%; text-align: right;"></td>
<td style="width: 10%; text-align: right;"></td>
<td style="width: 10%; text-align: right;"></td>
<td style="width: 10%; text-align: right;"></td>
</tr>
</tbody>
</table>
<p></p>
<p>Sheet2</p>
<table border="1" style="border-collapse: collapse; width: 100%;">
<tbody>
<tr style="height: 21px;">
<td style="width: 20%; text-align: center; height: 21px;">A</td>
<td style="width: 20%; text-align: center; height: 21px;">B</td>
<td style="width: 20%; text-align: center; height: 21px;">C</td>
<td style="width: 20%; text-align: center; height: 21px;">D</td>
<td style="width: 20%; text-align: center; height: 21px;">E</td>
</tr>
<tr style="height: 21px;">
<td style="width: 20%; height: 21px; text-align: center;">Item Name</td>
<td style="width: 20%; height: 21px; text-align: center;">ItemID</td>
<td style="width: 20%; height: 21px; text-align: center;">Material Name</td>
<td style="width: 20%; height: 21px; text-align: center;">MaterialID</td>
<td style="width: 20%; height: 21px; text-align: center;">Material QTY</td>
</tr>
<tr style="height: 21px;">
<td style="width: 20%; height: 21px; text-align: right;">A</td>
<td style="width: 20%; height: 21px; text-align: right;">1</td>
<td style="width: 20%; height: 21px; text-align: right;">Material 1</td>
<td style="width: 20%; height: 21px; text-align: right;">34</td>
<td style="width: 20%; height: 21px; text-align: right;">100</td>
</tr>
<tr style="height: 21px;">
<td style="width: 20%; height: 21px; text-align: right;">A</td>
<td style="width: 20%; height: 21px; text-align: right;">1</td>
<td style="width: 20%; height: 21px; text-align: right;">Material 2</td>
<td style="width: 20%; height: 21px; text-align: right;">36</td>
<td style="width: 20%; height: 21px; text-align: right;">150</td>
</tr>
<tr style="height: 21px;">
<td style="width: 20%; height: 21px; text-align: right;">A</td>
<td style="width: 20%; height: 21px; text-align: right;">1</td>
<td style="width: 20%; height: 21px; text-align: right;">Material 3</td>
<td style="width: 20%; height: 21px; text-align: right;">38</td>
<td style="width: 20%; height: 21px; text-align: right;">300</td>
</tr>
<tr style="height: 21px;">
<td style="width: 20%; height: 21px; text-align: right;">B</td>
<td style="width: 20%; height: 21px; text-align: right;">5</td>
<td style="width: 20%; height: 21px; text-align: right;">Material 1</td>
<td style="width: 20%; height: 21px; text-align: right;">34</td>
<td style="width: 20%; height: 21px; text-align: right;">200</td>
</tr>
<tr style="height: 21px;">
<td style="width: 20%; height: 21px; text-align: right;">B</td>
<td style="width: 20%; height: 21px; text-align: right;">5</td>
<td style="width: 20%; height: 21px; text-align: right;">Material 2</td>
<td style="width: 20%; height: 21px; text-align: right;">36</td>
<td style="width: 20%; height: 21px; text-align: right;">351</td>
</tr>
<tr style="height: 21px;">
<td style="width: 20%; height: 21px; text-align: right;">B</td>
<td style="width: 20%; height: 21px; text-align: right;">5</td>
<td style="width: 20%; height: 21px; text-align: right;">Material 5</td>
<td style="width: 20%; height: 21px; text-align: right;">40</td>
<td style="width: 20%; height: 21px; text-align: right;">255</td>
</tr>
<tr style="height: 21px;">
<td style="width: 20%; height: 21px; text-align: right;">C</td>
<td style="width: 20%; height: 21px; text-align: right;">6</td>
<td style="width: 20%; height: 21px; text-align: right;">Material 1</td>
<td style="width: 20%; height: 21px; text-align: right;">34</td>
<td style="width: 20%; height: 21px; text-align: right;">122</td>
</tr>
<tr style="height: 21px;">
<td style="width: 20%; height: 21px; text-align: right;">C</td>
<td style="width: 20%; height: 21px; text-align: right;">6</td>
<td style="width: 20%; height: 21px; text-align: right;">Material 8</td>
<td style="width: 20%; height: 21px; text-align: right;">60</td>
<td style="width: 20%; height: 21px; text-align: right;">134</td>
</tr>
</tbody>
</table>
<p></p>
<p>What i want is to look up the values in Sheet 2 so the Rows in Sheet 1 will look like this:</p>
<table border="1" style="border-collapse: collapse; width: 100%;">
<tbody>
<tr style="height: 21px;">
<td style="width: 10%; text-align: center; height: 21px;">A</td>
<td style="width: 10%; text-align: center; height: 21px;">B</td>
<td style="width: 10%; text-align: center; height: 21px;">C</td>
<td style="width: 10%; text-align: center; height: 21px;">D</td>
<td style="width: 10%; text-align: center; height: 21px;">E</td>
<td style="width: 10%; text-align: center; height: 21px;">F</td>
<td style="width: 10%; text-align: center; height: 21px;">G</td>
<td style="width: 10%; text-align: center; height: 21px;">H</td>
<td style="width: 10%; text-align: center; height: 21px;">I</td>
<td style="width: 10%; text-align: center; height: 21px;">J</td>
</tr>
<tr style="height: 21px;">
<td style="width: 10%; text-align: center; height: 21px;">Category</td>
<td style="width: 10%; text-align: center; height: 21px;">Item Name</td>
<td style="width: 10%; text-align: center; height: 21px;">Material 1</td>
<td style="width: 10%; text-align: center; height: 21px;">Material 2</td>
<td style="width: 10%; text-align: center; height: 21px;">Material 3</td>
<td style="width: 10%; text-align: center; height: 21px;">Material 4</td>
<td style="width: 10%; text-align: center; height: 21px;">Material 5</td>
<td style="width: 10%; text-align: center; height: 21px;">Material 6</td>
<td style="width: 10%; text-align: center; height: 21px;">Material 7</td>
<td style="width: 10%; text-align: center; height: 21px;">Material 8</td>
</tr>
<tr style="height: 21px;">
<td style="width: 10%; height: 21px; text-align: right;">Group A</td>
<td style="width: 10%; height: 21px; text-align: right;">A</td>
<td style="width: 10%; height: 21px; text-align: right;">100</td>
<td style="width: 10%; height: 21px; text-align: right;">150</td>
<td style="width: 10%; height: 21px; text-align: right;">300</td>
<td style="width: 10%; height: 21px; text-align: right;"></td>
<td style="width: 10%; height: 21px; text-align: right;"></td>
<td style="width: 10%; height: 21px; text-align: right;"></td>
<td style="width: 10%; height: 21px; text-align: right;"></td>
<td style="width: 10%; height: 21px; text-align: right;"></td>
</tr>
<tr>
<td style="width: 10%; text-align: right;">Group A</td>
<td style="width: 10%; text-align: right;">B</td>
<td style="width: 10%; text-align: right;">200</td>
<td style="width: 10%; text-align: right;">351</td>
<td style="width: 10%; text-align: right;"></td>
<td style="width: 10%; text-align: right;"></td>
<td style="width: 10%; text-align: right;">255</td>
<td style="width: 10%; text-align: right;"></td>
<td style="width: 10%; text-align: right;"></td>
<td style="width: 10%; text-align: right;"></td>
</tr>
<tr>
<td style="width: 10%; text-align: right;">Group A</td>
<td style="width: 10%; text-align: right;">C</td>
<td style="width: 10%; text-align: right;">122</td>
<td style="width: 10%; text-align: right;"></td>
<td style="width: 10%; text-align: right;"></td>
<td style="width: 10%; text-align: right;"></td>
<td style="width: 10%; text-align: right;"></td>
<td style="width: 10%; text-align: right;"></td>
<td style="width: 10%; text-align: right;"></td>
<td style="width: 10%; text-align: right;">134</td>
</tr>
</tbody>
</table>
<p></p>
答案 0 :(得分:1)
没有必要使用例过于复杂。根据:
params.dirname = hde,params.path = /usr/tmp/jenkins/hde/filename.txt或/usr/hde/jenkins/ing/filename.txt或任何包含hde的随机路径
您正在尝试查找给定字符串a
是否包含子字符串b
。可以使用Java的方法String.contains(String substring)
来完成。或者你也可以使用正则表达式,但是String.contains()
看起来简单了几次才能理解你的意图。请考虑遵循Groovy脚本:
def params = [
dirname: 'hde',
path: '/usr/tmp/jenkins/hde/filename.txt'
]
// Using String.contains()
if (params.path.contains(params.dirname)) {
println "Path '${params.path}' contains '${params.dirname}'"
}
// Using regular expression
if (params.path ==~ /(.*)${params.dirname}(.*)/) {
println "Path '${params.path}' contains '${params.dirname}'"
}
运行时,if
个语句的计算结果为true
:
Path '/usr/tmp/jenkins/hde/filename.txt' contains 'hde'
Path '/usr/tmp/jenkins/hde/filename.txt' contains 'hde'