问题描述
我需要按长度f.e从属性值中提取一个子字符串. :
I need to extract a substring from property value by length, f.e. :
<property name="prop1" value="nameBLABLABLA" />
我想要获得价值
name
是否可以不使用javascript代码?
Is it possible without using javascript code ?
推荐答案
不使用香草蚂蚁,您需要添加一些蚂蚁插件,例如
Antcontrib (最新发布的2006年!)或 Ant Flaka -表示您需要其他jar/库.
使用内置的jdk Javascript引擎,就像:
Not with vanilla ant, you would need to add some Ant addon like
Antcontrib (latest release 2006 !) or Ant Flaka - means you'll need additional jars/libraries.
With using the jdk builtin Javascript engine it's as easy as :
<project>
<!-- create a macrodef for reuse -->
<macrodef name="getsubstring">
<attribute name="src"/>
<attribute name="from"/>
<attribute name="to"/>
<attribute name="result"/>
<sequential>
<script language="javascript">
project.setProperty(
"@{result}", "@{src}".substring(@{from},@{to})
);
</script>
</sequential>
</macrodef>
<property name="foo" value="nameBLABLABLA"/>
<getsubstring src="${foo}" from="0" to="4" result="foobar"/>
<echo> $${foobar} => ${foobar}</echo>
</project>
不需要其他库.
创建了一个宏定义,该宏定义通常分别适用于字符串的属性.
JavaScript引擎了解Java语言和Java,您将获得对Ant api的完全访问权限.
No additional libraries needed.
Created a macrodef that works for properties respectively for strings in general.
The JavaScript engine understands Javascript and Java and you'll get full access to Ant api.
这篇关于蚂蚁子串按位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!